In C#, properties are nothing but natural extension of data fields. They are usually known as 'smart fields' in C# community. We know that data encapsulation and hiding are the two fundamental characteristics of any object oriented programming language.In C#, data encapsulation is possible through either classes or structures. By using various access modifiers like private, public, protected, internal etc it is possible to control the accessibility of the class members from outside of the Class using Properties.
Let's have an Example, to Understand what is Property and why we need properties in Our Program. In this example, we declare a Data Field as Private and will provide SET() and GET() Methods to Access the Data Fields. This is a Good Programming Practice, since the Data Fields are not directly accessible outside the class. we must use the SET/GET Methods to access the Data fields.
/*Example - Concept of Properties - InfoBrother*/
using System;
namespace Property
{
class Program
{
private int x;
public void SET(int i) //Set the value of x:
{
x = i;
}
public int GET() //Return the Value of x:
{
return x;
}
static void Main(string[] args)
{
Program obj = new Program(); //Object of type Program:
obj.SET(15); //set x = 15"
Console.WriteLine(" The value of X is: " + obj.GET());
Console.ReadKey();
}
}
}
Properties
The value of X is: 15
In Above, We write simple example to set the value of X using SET() Method, and read that value of X using GET() Method. We write these methods ourselves, but C# Provides a Built in Mechanism to do the Above Task, Called Properties.
Properties:
Properties are Named Members of Classes, Structure, and Interface. Member Variables or Method in a Class or Structure are Known as Fields and properties are the Extension of fields. The Properties Use Accessors through which the Values of the Private fields can be Read, Written, or Manipulated. The Properties Do not Name the Storage locations. instead, they have Accessors that read, write, or Compute their Values.
The Accessor Declaration can Contain a GET , SET or Both Accessor. The General Form of Declaring a Property is as Follows.
Access_Modifier Return_Type Property_Name
{
get
{
//body:
}
set
{
//body:
}
}
Let's have an Simple Example to Understand the concept of Properties, and how its work. In this example we will Create an Separate class, and inside the class We define some private properties. Now using SET and GET Accessors, we will access the Private Properties of the class from outside. Look at the example -
/*Example - Properties - InfoBrother*/
using System;
namespace Property
{
class Teacher //Class Teacher:
{
//Private Members:
private string name = "Anonymous";
private int id = 0;
private string expertIn = "Don't know";
//Declare the Name Property of Type String:
public string NAME
{
get //Return Name of teacher:
{
return name;
}
set //Set the Name of Teacher:
{
name = value;
}
}
//Declare the ID Property of Type Int:
public int ID
{
get //Return the ID of Teacher:
{
return id;
}
set //Set the ID of Teacher:
{
id = value;
}
}
//Declare the expertIn Property of Type String:
public string Expert_IN
{
get //Get the Subject of Teacher:
{
return expertIn;
}
set //Set the subject of teacher:
{
expertIn = value;
}
}
public override string ToString() //We override the ToString() Method:
{
return "Name = " + NAME + ", ID = " + ID + ", Expert In = " + Expert_IN;
}
}
class Program
{
static void Main(string[] args)
{
Teacher teach = new Teacher(); //Object of type Teacher:
//1st teacher info:
teach.NAME = "InfoBrother";
teach.ID = 18475;
teach.Expert_IN = "C# Programming";
Console.WriteLine("1): " + teach); //output
//Second teacher Info:
teach.NAME = "Sardar Omar";
teach.ID = 13121;
teach.Expert_IN = "C++ Programming";
Console.WriteLine("2): " + teach); //output
Console.ReadKey();
}
}
}
Using Properties:
1): Name = InfoBrother, ID = 18475, Expert In = C# Programming
2): Name = Sardar Omar, ID = 13121, Expert In = C++ Programming
“
In the Above example, did you Notice that, we use one variable value in our example without declaring it. Actually it is an C# Reserved word. this keyword value is freely available in Our Example, which gets created Automatically by the compiler. and we can't declare any variable with the same name "value" inside the SET accessor.