|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionHere we are going to introduce you to the concept of properties in C#. Properties can help simplify programming aspects. Let us understand what these programming aspects are and then understand how properties can be of help to simplify these programming aspects. Let us understand the programming aspects that properties intend to simplify, step by step through small programs. What makes us use Properties?Let's write a simple C# program that implements a class named P1.csclass Maths
{
public static void Main ()
{
}
}
The program compiles successfully. Cool! Now let us add a private integer P2.csclass Maths
{
private int val;
public static void Main ()
{
}
}
OutputP2.cs(3,15): warning CS0169: The private field 'Maths.val' is never used
The program compiles successfully but it throws a warning message. The warning says that we haven't used the private variable P3.csclass Maths
{
private int val;
public static void Main ()
{
}
public void SetVal ( int x )
{
val = x;
}
}
OutputBingo! This program compiles successfully and that too without any warning message. The warning disappeared because the private variable P4.csclass Maths
{
private int val;
public static void Main ()
{
}
public void SetVal ( int x )
{
val = x;
}
public void Display ( int x )
{
System.Console.WriteLine(val);
}
}
OutputThis program compiles successfully but we don't get any output. That's because the P5.csclass Maths
{
private int val;
public static void Main ()
{
Maths obj = new Maths();
obj.Display();
}
public void SetVal ( int x )
{
val = x;
}
public void Display()
{
System.Console.WriteLine(val);
}
}
Output0
The program compiles and runs successfully to output the value of private member variable P6.csclass Maths
{
private int val;
public static void Main ()
{
Maths obj = new Maths();
obj.Display();
}
public void SetVal ( int x )
{
val = x;
}
public void Display()
{
System.Console.WriteLine(val);
}
public int GetVal ()
{
return val;
}
}
Output0
As expected the program compiles and runs successfully to output the value of private member variable P7.csclass Maths
{
private int val;
public static void Main ()
{
int y = 0;
Maths obj = new Maths();
obj.Display();
obj.SetVal(10);
y = obj.GetVal();
System.Console.WriteLine(y);
obj.SetVal(20);
y = obj.GetVal();
System.Console.WriteLine( y );
obj.SetVal(30);
y = obj.GetVal();
System.Console.WriteLine( y );
}
public void SetVal ( int x )
{
val = x;
}
public void Display()
{
System.Console.WriteLine(val);
}
public int GetVal ()
{
return val;
}
}
Output0
10
20
30
The program compiles and runs successfully. Inside In the above program, every time we had to set the value of the private member variable obj.SetVal(10);
obj.SetVal(20);
obj.SetVal(30);
Wouldn't it be of convenience if we could write the above statements as follows to achieve the same results? obj.val = 10;
obj.val = 20;
obj.val = 30;
Certainly yes! Similarly, in the above program, every time we had to get the value of the private member variable val we had to write the statements like y = obj.GetVal();
y = obj.GetVal();
y = obj.GetVal();
Wouldn't it be of convenience if we could write the above statements as follows to achieve the same results? y =obj.val ;
y =obj.val ;
y =obj.val ;
Certainly yes! But hold on! Didn't you notice that from the Let's use PropertiesWe will define a property for the private member variable obj.Val = 10;
y =obj.Val ;
Doesn't this look very handy? Certainly it does! So let's enhance the above program to make use of Properties. Let's do things step by step. Our first step is to remove the functions P8.csclass Maths
{
private int val;
public static void Main ()
{
int y = 0;
Maths obj = new Maths();
}
}
OutputP8.cs(7,10): warning CS0219: The variable 'y' is assigned but its value is
never used
P8.cs(3,15): warning CS0169: The private field 'Maths.val' is never used
For now let us live with the warnings. Now let's enhance the above program by adding a property P9.csclass Maths
{
private int val;
public static void Main ()
{
int y = 0;
Maths obj = new Maths();
}
public int Val
{
get
{
return val;
}
set
{
val = value;
}
}
}
OutputP9.cs(7,9): warning CS0219: The variable 'y' is assigned but its value is never
used
The program compiles successfully. This time it throws only one warning. The code in the bold font represents the property int func ()
{
//code
}
Observe the parenthesis in bold above. Now imagine how would a property of name int func
{
//code
}
Observe the absence of parenthesis in the property definition above. Now let's have a look at the program above. It defines a property public int Val
{
//code
}
Simple isn't it?Now let's focus on the code inside the property public int Val
{
get
{
return val;
}
set
{
val = value;
}
}
It contains two accessors named Now let us focus on the get
{
return val;
}
As mentioned earlier, the Now let us focus on the set
{
val = value;
}
As mentioned earlier, the Lastly, let us understand Now let's enhance the above program to put the defined property to use as well as add a PREVIOUS function P10.csclass Maths
{
private int val;
public static void Main ()
{
int y = 0;
Maths obj = new Maths();
obj.Val = 100;
y = obj.Val;
obj.Display();
}
public void Display()
{
System.Console.WriteLine(val);
}
public int Val
{
get
{
return val;
}
set
{
val = value;
}
}
}
Output100
The above program compiles and runs successfully to output the value of the private member variable obj.Val = 100;
the val = value;
This code assigns the value of variable Let us enhance the above program by adding some print statements to get a firm grip. P11.csclass Maths
{
private int val;
public static void Main ()
{
int y = 0;
Maths obj = new Maths();
System.Console.WriteLine("Before set call");
obj.Val = 100;
System.Console.WriteLine("After set call");
System.Console.WriteLine("Before get call");
y = obj.Val;
System.Console.WriteLine("After get call");
obj.Display();
}
public void Display()
{
System.Console.WriteLine(val);
}
public int Val
{
get
{
System.Console.WriteLine("Inside get");
return val;
}
set
{
System.Console.WriteLine("Inside set");
val = value;
}
}
}
OutputBefore set call
Inside set
After set call
Before get call
Inside get
After get call
100
The above program compiles and executes successfully. The output clearly proves that when the following statement is getting executed: obj.Val = 100;
the The output also clearly proves that when the following statement is getting executed: y =obj.Val;
the With this I hope that you have understood the basic concept of property. Now let's go through a simple exercise to gain a firm understanding of property. Let us define a class called P12.cs// Class cable
class cable
{
private int length;
public static void Main ()
{
int y = 0;
cable CoaxialCable = new cable();
System.Console.WriteLine("Before set call");
CoaxialCable.Length = 100;
System.Console.WriteLine("After set call");
System.Console.WriteLine("Before get call");
y = CoaxialCable. Length;
System.Console.WriteLine("After get call");
obj.Display();
}
//Display function to display the private member variable length
public void Display()
{
System.Console.WriteLine(length);
}
//Property Length
public int Length
{
get
{
System.Console.WriteLine("Inside get");
return val;
}
set
{
System.Console.WriteLine("Inside set");
val = value;
}
}
}
OutputBefore set call
Inside set
After set call
Before get call
Inside get
After get call
100
By now the above program must have become a self-explainatory one for you. So let's move on… Read Only PropertiesRead Only Properties are the properties with only get accessor and no set accessor. Checkout the following program. P13.csclass cable
{
private int length;
public static void Main ()
{
int y = 0;
cable CoaxialCable = new cable();
System.Console.WriteLine("Before get call");
y = CoaxialCable. Length;
System.Console.WriteLine("After get call");
obj.Display();
}
public void Display()
{
System.Console.WriteLine(length);
}
public int Length
{
get
{
System.Console.WriteLine("Inside get");
return val;
}
}
}
OutputBefore get call
Inside get
After get call
0
The above program compiles and runs successfully. Observe that property Write Only PropertiesWrite Only Properties are the properties with only set accessor and no get accessor. Checkout the following program. P14.csclass cable
{
private int length;
public static void Main ()
{
int y = 0;
cable CoaxialCable = new cable();
System.Console.WriteLine("Before set call");
CoaxialCable.Length = 100;
System.Console.WriteLine("After set call");
obj.Display();
System.Console.WriteLine("Before set call");
CoaxialCable.Length = 200;
System.Console.WriteLine("After set call");
obj.Display();
}
public void Display()
{
System.Console.WriteLine(length);
}
public int Length
{
set
{
System.Console.WriteLine("Inside set");
val = value;
}
}
}
OutputBefore set call
Inside set
After set call
100
Before set call
Inside set
After set call
200
The above program compiles and runs successfully. Observe that property
|
|||||||||||||||||||||||||||||||||||||||||||||||||||