• Have Any Question?
  • contact@eitworld.com

csharp properties





C# Properties

 

  • There is a situation when we declare all the members of class as private. Then problem is that how can we access these private fields? This can be done by the use of ‘Properties’.
  • C# is a first language which introduced the concept of ‘Properties’.
  • Properties use the accessors in order to access the private fields. This accessor contains getter (to retrieve value of private field) and setter (to assign value to private field).
  • The basic syntax for properties is as shown below:

General Syntax of Properties

 

{
get
{
//some optional statement
return ;
}
set
{
//some optional statement
=value;
}

    • Let us take an example to understand the Properties:

We have a private field ‘name’ as:

 

private string name;

    • Now here we define the Property for this private field name by setting its getter and setter as follows:
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}

    • In the above example we defined a Property ‘Name’ which is accessing a private field name.
    • It is becoming a convention to name the property as corresponding field but with first letter in uppercase (like for name->Name and for age->Age).
    • As properties are accessor of a field so it mostly marked as public.
    • Finally in the set block we assign the field name with value, here value is a keyword which contains the value passed when the property is called.

Here is an example of properties which is sufficient to understand the concept of Properties:

 

Example

 

using System;
namespace propertiesexample
{
class Employee
{
private string name;
private int age;
//Declare a Name property of type string
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
//Declare a Age property of type int
public int Age
{
get
{
return age;
}
set
{
age=value;
}
}
public override string ToString() { return ” Name = ” + Name + “, Age = ” + Age; }

static void Main()
{

Employee e=new Employee();
//Setting the name and age of the employee e
e.name=”Radha”;
e.age=23;
Console.WriteLine(“Employee Information:{0}”,e);
Console.ReadLine();
}
}
}

Note: In the above example “ public override string ToString() { return ” Name = ” + Name + “, Age = ” + Age; }” is shows the overridden of ToString() method and every class or struct in C# implicitly inherits the Object class. Therefore, every object in C# gets the ToString method, which returns a string representation of that object. For example, all variables of type int have a ToString method, which enables them to return their contents as a string.

 

The ouput of the above program is as follows:

 

Output

 

Employee Information: Name=Radha, Age=23

Precautions while using Properties

 

    • Properties don’t have argument lists; set, get and values are keyword in C#.
    • The data-type of value should be same as the type of Property.
    • Always use proper curly bracket{} while using Properties.
    • Don’t try to write set{} and get{} in a single line.


Real Time Web Analytics