Tuesday, March 11, 2008

Automatic Properties

I was writing a piece of test code in VS2008 and the prop code snippet want just generate the private field, did some goggling around and found this out.
Well, you may remember in the days of VS2003 when you had to write your private fields and then write the property getter and setter for it, that was cumbersome, I remember when it became to hard for me to write all these properties and I had to write a tool to generate the properties (not a very cool tool though). What’s worse some properties only set the passed parameter into the private field and return it in the getter, like this

private string studenName_;

public string StudentName

{

get {return studentName_;}

set {studentName_ = value;}

}

In .Net 3.5 , through a feature called Automatic properties you can write the above code as

public string StudentName { get; set; } and

at compile time the compiler will emite code to construct a privet field.

No comments:

Post a Comment