Fields and Properties
Add CommentIntroduction
C# has a concept of Properties which has been borrowed from Visual Basic. This concept is new for C/C++ and Java programmers. This is a very useful concept while developing object oriented class libraries hence let's have a look at it.
Fields
In OOP (Object Oriented Programming), the Class is the biggest entity. The class encapsulates (contains) everything within it.
The variables that you declare as global within a class as member variables are called
Fields.
public class MyClass{
public int i =10 ; //this is a field
}
You can access these fields of the above class as given below:
public class MyConsumer {
public static void Main()
{
MyClass mc = new MyClass(); //Make a instance of MyClass
System.Console.WriteLine(mc.i) ; //Get the field 'i' of MyClass
mc.i=20 ; //Set the value of field 'i' of MyClass
System.Console.WriteLine(mc.i); //Get the field 'i' of MyClass
}
}
This seems very easy, but if you go according to OOP's (Object
Oriented Programming) standards, member fields should always be private and should only have access through some
public interfaces.
In Java, to follow the above rule we got used to writing separate GetXX and SetXX methods to
retrieve and set the private field's value.
So our example in Java style will now become.
public class MyClass{
private int i =10 ; //this is a private field
//Get method
public int GetI()
{
return i; //return the value
}
//Set method
public void SetI(int n)
{
this.i= n; //set the value
}
}
public class MyConsumer {
public static void Main()
{
MyClass mc = new MyClass(); //Make a instance of MyClass
System.Console.WriteLine(mc.GetI()) ; //Call the GetI method to get the value of 'i'
mc.SetI(20) ; //call the method SetI to set the value of 'i'
System.Console.WriteLine(mc.GetI()); //Call the method GetI to get the value of 'i'
}
}
This did solve the problem and was widely used. But it gave rise to a new problem! If you had a
huge class there were so many GetXX and SetXX methods that it became very difficult for the programmer to
keep a tack of all these methods.
Also many times the GetXX method would be at one place and the SetXX
method at another, making the programmers virtually hunt for the
methods!
Solution
Microsoft has realized this problem and they have implemented Properties.
Properties are a way to access member fields which looks similar to accessing a
field directly, but internally the Get/Set property accessors are called.
Property definition:-
<access modifier> <return type> <Property name> {
//optional
get
{
....
}
//optional
set
{
...
}
}
so our example in C# using properties changes too:
public class MyClass{
private int i =10 ; //this is a private field
// I property
//Remember properties don't have open/close brackets '()' after their definition.
//Hence they cannot have any input parameters like Methods
//Only one default input variable is available in the 'set' accessor. The name of this variable is 'value' .
//The 'value' variable has the same data type as the return type used in the property
definition.
public int I
{
//get accessor for Read access
get{
return i; //return the value
}
//set accessor for Write access
set{
this.i= value; //set the value
}
}
public class MyConsumer {
public static void Main()
{
MyClass mc = new MyClass(); //Make a instance of MyClass
System.Console.WriteLine(mc.I) ; //Call the 'I' property to get the value of 'i'.
mc.I=20 ; //Call the 'I' property to set the value of 'i'.
System.Console.WriteLine(mc.I); //Call the 'I' property to get the value of 'i'.
}
}
If you see the above example, the MyConsumer class seems very similar to the first case we had taken. The consumer accesses a property in the same way it accesses member fields. This makes the use of properties very easy.
get / set accessors.
There are two accessors that you can use in your properties, namely
get and set. You
can use both or either one of them while defining your property.
get - accessor provides read-only access to your field.
Example: If you are writing a bank class, the Bank Account Number should never be changed by the client, hence in the property for the Bank Account Number just define the
get accessor so that clients can only read the bank account number.
set - accessor provides write-only access to your field.
You can use any of these accessors singly or you can define both of them together depending upon the type of access you need.
Another important feature of properties is that it allows you to check the
values that are been set.
Example: You have a calendar class. The month attribute should always be a value between 1 and 12,
right? You can use Properties to enforce this logic.
public int Month
{
set{
if(value>0 && value<13)//check if month is in the valid range
this.month=value ;
}
}
Another important inherent feature that Properties give you is that its not necessary that the source of information be a field, you can get and set values into a database using properties and the end-user feels as if he is only accessing a field! Thus in future if you want to change the internal logic of your property you can easily do so without breaking the clients code!
Conclusion
You have seen the importance and ease of use of properties in C#. Properties
are extensively used in Windows Forms API to rapidly build GUI
applications. Every Control has a defined set of properties that can be set
using the Property Explorer at design time making the design model very easy and
powerful.

