Declaring a property in C# 3.0 is super easy and super short.
public class Student
{
public string Name { get; set; }
}
yes that's it, the framework will take care of the rest, the private variables will be automatically created and the getter and setter will be automatically implemented.
Here is how we can assign value to an automatic property via the constructor
public class Student
{
public string Name { get; set; }
public Student (string name)
{
this.Name = name;
}
}
And finally, here is how we can declare a Readonly property
public class Student
{
public string Name { get; private set; }
public Student (string name)
{
this.Name = name;
}
}
Hope this helps, Enjoy coding.