What are Auto-Implemented Properties in .NET?
In .NET 3.0, these were created to make coding easier and quicker. When you define a property previously, you had to put in code to store the value in a private variable. With AIP, you don't need to do that. In the below example, FirstName is using the traditional way of properties whereas Surname is an Auto-Implemented Property.
public class Person
{
private string m_strFirstName = "";
public string FirstName
{
get { return m_strFirstName; }
set { m_strFirstName = value; }
}
public string Surname { get; set; }
public string FullName()
{
return m_strFirstName + " " + Surname;
}
}
class Program
{
static void Main(string[] args)
{
Person A = new Person();
A.FirstName = "Joe";
A.Surname = "Bloggs";
Console.WriteLine("Employee :- " + A.FullName().ToString());
Console.ReadLine();
}
}
Tags - .NET
Last Modified : June 2023