Auto-implemented properties is the new feature of C# 3.0 new feature to create property-declaration more concise.
What is Property: Property is a way to initialize the private member variables of class. We use get and set accessor to add data into variables and to show their values.
GET accessor: It is used to return class member variable value. It is also used to write only property(member variable of class) as well.
SET accessor : It is used to set value into class member variable value.It is also used to read only property(member variable of class) as well.
Example :
Program Class :
class Class1
{
//private string _name;
//public void Name(string name)
//{
// this._name = name;
//}
//public string ShowName()
//{
// return this._name;
//}
//public string Name
//{
// get { return _name; }
// set { _name = value; }
//}
public string Name { get; set;}
Main Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Class1 c = new Class1();
// c.Name(“Yogesh Sharma”);
//Console.WriteLine(c.ShowName());
//Console.ReadLine();
//c.Name = “Vikas Sharma”;
//Console.WriteLine(c.Name);
//Console.ReadLine();
c.Name = “Harsh Sharma”;
Console.WriteLine(c.Name);
Console.ReadLine();
}
}
}