Welcome to Bangladesh Microsoft Technology Community Sign in | Join | Help

C# 3.0 tips, Automatic Property

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.

Published Wednesday, June 11, 2008 9:21 AM by Shahed

Comments

No Comments

Anonymous comments are disabled