Properties in C#
Properties in C#
In C# a concept is known as properties know what are the propertites?
As we viewed in the previous post the Data members are private in c# so they are not accessible outside the class So this concept came into picture about accesing the data members that is reading and writing purpose one can say to get and set the variable's values by the properties which are private.
Now moving on to the basics of properties in C#
first we look at the examples
Class abc
{
int qty;
public abc()
{
a=0;
}
public int Qty//property declared
{
get//get accessor declared
{
return qty;
}
set//set accessor declared
{
qty=value;
}
}
The above example reveals the architecture of property in C# it has the accessmodifier that is public,private, protected etc, followed by the datatype of the variable you want to initialize here in the above example we want to initialize the integer Data type variable so public int Know ahead comes the property name its the common convention that you give the same Name to the property as the variable name but the first Letter with uppercase so any type of ambiguity is not Generated know combining this becomes our first statement of our property that is public int Qty.
Inside it contains the get and set accessor get is used when you want to read the values of the variables and set is used when we want to initialize the values of that variable.
Note:-we can only write the get accessor in the property that means the property is only used for reading purpose not for writing purpose.And if we write only set accessor is used only to set values.
for public and private accessmodifiers inside you can also use with get and set but in above example both are public as we have mentioned none but ,can make any one among them as private not both otherwise there will be no meaning of the public we wrote in front of the property name.
Now how to access these ....
Now we have other class say pqr
class pqr
{
public static void Main()//M is capital in C#
{
abc o1=new abc();//declaring the object of class abc
int x=o1.qty=4;\\ Calls the set accessor in property
System.Console.ReadLine("initialized value is "+x);
}
}
That was all for todays post ..!!
Give your valuable feedback ..!!
public int Qty//property declared
{
get//get accessor declared
{
return qty;
}
set//set accessor declared
{
qty=value;
}
}
The above example reveals the architecture of property in C# it has the accessmodifier that is public,private, protected etc, followed by the datatype of the variable you want to initialize here in the above example we want to initialize the integer Data type variable so public int Know ahead comes the property name its the common convention that you give the same Name to the property as the variable name but the first Letter with uppercase so any type of ambiguity is not Generated know combining this becomes our first statement of our property that is public int Qty.
Inside it contains the get and set accessor get is used when you want to read the values of the variables and set is used when we want to initialize the values of that variable.
Note:-we can only write the get accessor in the property that means the property is only used for reading purpose not for writing purpose.And if we write only set accessor is used only to set values.
for public and private accessmodifiers inside you can also use with get and set but in above example both are public as we have mentioned none but ,can make any one among them as private not both otherwise there will be no meaning of the public we wrote in front of the property name.
Now how to access these ....
Now we have other class say pqr
class pqr
{
public static void Main()//M is capital in C#
{
abc o1=new abc();//declaring the object of class abc
int x=o1.qty=4;\\ Calls the set accessor in property
System.Console.ReadLine("initialized value is "+x);
}
}
That was all for todays post ..!!
Give your valuable feedback ..!!
Comments
Post a Comment