Accessors property are use to read, write, or compute their values. Basically property are a member of classes, structures, and interfaces. The accessor property of a contains the executable statements that helps in getting or setting the property.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace accessors_property_in_c_sharp
{
class User_detail
{
private int id = 0;
private string name = "not known";
private string city = "N.A";
public int Id // Declare a Id property of type int:
{
get
{
return id;
}
set
{
id = value;
}
}
public string Name // Declare a Name property of type string:
{
get
{
return name;
}
set
{
name = value;
}
}
public string City // Declare a City property of type string:
{
get
{
return city;
}
set
{
city = value;
}
}
public override string ToString()
{
return " Id = " + Id+ ", City = ," + City + ", Name = " + Name ;
}
public static void Main()
{
User_detail ud = new User_detail(); // Create a new uesr_detail object:
ud.Id = 100; // Setting Id, Name and the City of the user_detail
ud.Name = "Arvind";
ud.City = "Noida";
Console.WriteLine("User Info: {0}", ud);
ud.Id += 1; //let us increase Id
Console.WriteLine("User Info: {0}", ud);
}
}
}
Output:


Join the conversation! Your thoughts help the community grow.