Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public vs Private{get, set}

Tags:

c#

.net

I'm making a new class and I caught myself wondering: Is there any difference between writing:

public string temp;

And writing:

private string temp;
public string temp_
{
get { return temp; }
set { temp = value; }
}

I'm guessing this is kind of a newbie question, but I didn't find an absolute answer... In both cases it seems I can access the object outside the class. Is the private form just a sloppy way?

like image 543
Shachar Levavi Avatar asked Jan 15 '17 08:01

Shachar Levavi


2 Answers

Yes, the difference is that you have a property. Properties in C# are a syntax sugar over having a pair of get and set method. The compiler in fact takes the code blocks and creates two separate methods: get_PropertyName and set_PropertyName, where PropertyName is the name of your property. These methods have the exact logic you implement in the get and set blocks respectively, and the code using your property will in fact be calling these methods behind the scenes.

The advantage of this is that you have full control over the way the value is set and retrieved.

Example - imagine you want to store age:

public int Age;

Now anyone using your class can easily set the age to any value they please - 1000, -1, anything.

Now if you have a property, you can make sure that doesn't happen:

private int _age = 0;

public int Age
{
   get { return _age; }
   set { if ( value >= 0 && value < 120 ) _age = value; }
}

Most of the time it is beneficial to declare public fields as public properties even though you don't need any validation logic, because you might need to add one in the future and if you act proactively by creating a property in the first place, other code using your library won't need to be recompiled to work.

Properties also give you more fine-grained control over the visibility of getters and setters. You can have a public property with public getter and private setter to make sure only the class itself can change the state of the property.

public Connected { get; private set; }

In addition, there are places where you really need to have a property. One of those is using the INotifyPropertyChanged interface in the MVVM pattern for WPF, UWP and others. Data binding requires a property to be bound to (although this is not completely true if you don't need notifications, as the new {x:Bind} syntax in UWP can bind to ordinary fields).

like image 160
Martin Zikmund Avatar answered Nov 05 '22 06:11

Martin Zikmund


If you use this one

public string Age;

you can set the variable to any value Age = "99999999" .

if you use this

private string Age;
    public string Age_
    {
    get { return temp; }
    set { if ( value >= 0 && value < 120 ) temp = value;
         else  // write some message
 }
    }

you can write some code inside . and make some tests Before you set or get the value of the variable

like image 34
zakaria kasmi Avatar answered Nov 05 '22 07:11

zakaria kasmi