Fields and Properties
Add Comment<span class="wboxheado">Introduction</span><br> C# has a concept of <b> Properties</b> 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.<p><span class="wboxheado">Fields</span> <br> In OOP (Object Oriented Programming), the <b>Class</b> is the biggest entity. The class encapsulates (contains) everything within it.<br> The variables that you declare as global within a class as member variables are called <i>Fields</i>. </p> <p><span class="codetext">public class MyClass{<br> public int i =10 ; //this is a field<br> }</span> </p> <p>You can access these fields of the above class as given below: </p> <p><span class="codetext">public class MyConsumer {<br> <br> public static void Main()<br> {<br> MyClass mc = new MyClass(); //Make a instance of MyClass<br> System.Console.WriteLine(mc.i) ; //Get the field 'i' of MyClass<br> mc.i=20 ; //Set the value of field 'i' of MyClass<br> System.Console.WriteLine(mc.i); //Get the field 'i' of MyClass<br> }<br> }</span> </p> <p>This seems very easy, but if you go according to OOP's (Object Oriented Programming) standards, member fields should always be <b> private</b> and should only have access through some <b> public interfaces</b>.<br> 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.<br> <br> So our example in Java style will now become. </p> <p><span class="codetext">public class MyClass{<br> private int i =10 ; //this is a private field<br> //Get method<br> public int GetI()<br> {<br> return i; //return the value<br> }<br> //Set method<br> public void SetI(int n)<br> {<br> this.i= n; //set the value<br> }<br> } </p> <p>public class MyConsumer {<br> <br> public static void Main()<br> {<br> MyClass mc = new MyClass(); //Make a instance of MyClass<br> System.Console.WriteLine(mc.GetI()) ; //Call the GetI method to get the value of 'i'<br> mc.SetI(20) ; //call the method SetI to set the value of 'i'<br> System.Console.WriteLine(mc.GetI()); //Call the method GetI to get the value of 'i'<br> }<br> }</span> </p> <p>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!<br> <br> <span class="wboxheado">Solution</span><br> Microsoft has realized this problem and they have implemented <b>Properties</b>. 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.<br> <br> <i> Property definition:-</i><br><span class="codetext"> <access modifier> <return type> <Property name> {<br> //optional<br> get<br> {<br> ....<br> }<br> //optional<br> set<br> {<br> ...<br> }<br> }</span><br> <br> so our example in C# using properties changes too:<br> <br><span class="codetext"> public class MyClass{<br> private int i =10 ; //this is a private field<br> // I property<br> //Remember properties don't have open/close brackets '()' after their definition.<br> //Hence they cannot have any input parameters like Methods<br> //Only one default input variable is available in the 'set' accessor. The name of this variable is 'value' .<br> //The 'value' variable has the same data type as the return type used in the property definition.<br> public int I<br> {<br> //get accessor for Read access<br> get{<br> return i; //return the value<br> }<br> //set accessor for Write access<br> set{<br> this.i= value; //set the value<br> }<br> }<br> <br> public class MyConsumer {<br> <br> public static void Main()<br> {<br> MyClass mc = new MyClass(); //Make a instance of MyClass<br> System.Console.WriteLine(mc.I) ; //Call the 'I' property to get the value of 'i'.<br> mc.I=20 ; //Call the 'I' property to set the value of 'i'.<br> System.Console.WriteLine(mc.I); //Call the 'I' property to get the value of 'i'.<br> }<br> }</span><br> <br> If you see the above example, the <i> MyConsumer</i> 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.<br> <br> <span class="wboxheado">get / set accessors.</span><br> There are two accessors that you can use in your properties, namely <b>get</b> and <b>set</b>. You can use both or either one of them while defining your property.<br> <br> <b> get</b> - accessor provides read-only access to your field.<br> <br> 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 <i>get</i> accessor so that clients can only read the bank account number.<br> <br> <b> set</b> - accessor provides write-only access to your field.<br> <br> You can use any of these accessors singly or you can define both of them together depending upon the type of access you need.<br> Another important feature of properties is that it allows you to check the values that are been set. </p> <p>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.<br> <br> <span class="codetext">public int Month<br> {<br> set{<br> if(value>0 && value<13)//check if month is in the valid range<br> this.month=value ;<br> }<br> }</span> </p> <p>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<font color="#FF0000"> </font>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! </p> <p><span class="wboxheado">Conclusion</span><br> 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.