Use getter setter shorthand declarations

Akiii
Posted by Akiii under C# category on | Points: 40 | Views : 2621
public string MyString { get;set; };


which creates a private variable with the default public accessors (getters/setters), which is basically equivalent to...

private string _myString;

public string MyString
{
get { return _myString; }
set { _myString = value; }
}



Use the above for code simplicity. I think, it has been there from the C# v3.5 version. Correct me if i am wrong.


Thanks and Regards
Akiii

Comments or Responses

Posted by: Ogipansrk on: 2/10/2012 Level:Starter | Status: [Member] | Points: 10
Akii ,

You are right , I would like to extend this post.

If you are using MVVM pattern , then you should follow the
private string _myString;



public string MyString

{

get { return _myString; }

set { _myString = value; }

}

shortcut won't wokr here.

Hope diz helps!!!!
Posted by: Akiii on: 2/10/2012 Level:Bronze | Status: [Member] | Points: 10
Is { get;set; }; not needed in MVVM framework ?

can you explain a bit clearly ?


Regards
Akiii

Login to post response