web analytics

Understanding Properties in C#

Options

codeling 1602 - 6666
@2016-01-13 13:48:49

In C#, properties are named members of classes, structs, and interfaces. They provide a flexible mechanism to read, write, or compute the values of private fields through accessors.

Properties are an extension of fields and are accessed using the same syntax. Unlike fields, properties do not designate storage locations. Instead, properties have accessors that read, write, or compute their values.

In C#, properties are defined using the property declaration syntax. The general form of declaring a property is as follows.          

<acces_modifier> <return_type> <property_name>
{
    get
    {
    }
    set
    {
    }
}

Where <access_modifier> can be private, public, protected or internal. The <return_type> can be any valid C# type. Note that the first part of the syntax looks quite similar to a field declaration and second part consists of a get accessor and a set accessor. 

For example the above program can be modifies with a property X as follows.

class MyClass
{
 private int _x;
 
 public int X
 {
  get
  {
   return _x;
  }
  set
  {
   _x = value;
  }
 }
}
@2016-01-13 14:12:18

Static Properties

C# also supports static properties, which belongs to the class rather than to the objects of the class. All the rules applicable to a static member are applicable to static properties also.

The follwing example demonstrates instance, static, and read-only properties. It accepts the name of the employee from the keyboard, increments numberOfEmployees by 1, and displays the Employee name and number.

using System;

public class Employee
{
   public static int numberOfEmployees;
   private static int counter;
   private string name;
   // A read-write instance property:
   public string Name
   {
      get
      {
         return name;
      }
      set
      {
         name = value;
      }
   }

   // A read-only static property:
   public static int Counter
   {
      get
      {
         return counter;
      }
   }
   // Constructor:
   public Employee()
   {
      // Calculate the employee's number:
      counter = ++counter + numberOfEmployees;
   }
}

public class MainClass
{
   public static void Main()
   {
      Employee.numberOfEmployees = 100;
      Employee e1 = new Employee();
      e1.Name = "Claude Vige"; 
      Console.WriteLine("Employee number: {0}", Employee.Counter);
      Console.WriteLine("Employee name: {0}", e1.Name);
   }
}

Output

Employee number: 101
Employee name: Claude Vige
@2016-01-13 14:52:54

Property name mistakes

When implementing properties, be careful that the property name is distinct from the data member used in the class. It is easy to accidentally use the same name and trigger infinite recursion when the property is accessed.

// The following code will trigger infinite recursion

private string name;

public string Name

{

  get

  {

     return Name; // should reference “name” instead.

  }

}
@2016-02-01 12:51:15

Automatic properties

With normal properties that get and set mapping to the private field, you can do something like:

private string _name;

public string Name

{

    get

    {

    return _name;

    }

    set

    {

    _name= value;

    }

}

With a simple property like that, we could pretty much just as well have declared the field as public and used it directly, instead of adding the extra layer of a property. Automatic properties allows you to create something really concise:

public string Name

{

      get;

      set;

}

No field declaration, and no code to get and set the value of the field. All of that is handled automatically by the compiler, which will automatically create a private field and populate the get and set method with the basic code required to read and write the field.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com