Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use member variables in a class

I've seen most people use member variables in a class like :

string _foo;
public string foo { get { return _foo; }; private set { _foo = value}; }

But what's the difference of that to this?

public string foo { get; private set; }
like image 634
CudoX Avatar asked Feb 25 '14 10:02

CudoX


Video Answer


1 Answers

In simple cases like that its the same but in more complex cases where you fire events or something you need additional code in the get and set so you need the member ex:

private string _name;
public string Name
{
   get{ return _name; }
   set
   {
      SomeHandler("Name", value);
      _name = value;
   }
}
like image 124
Pedro.The.Kid Avatar answered Oct 27 '22 00:10

Pedro.The.Kid