C
C#•3y ago
LDami

Is there any benefit to write a Property with a private variable ?

Why:
private int myVar;
public int MyVar { get => myVar; set => myVar = value; }
private int myVar;
public int MyVar { get => myVar; set => myVar = value; }
may be better than:
public int MyVar;
public int MyVar;
if we let get and set public ?
26 Replies
becquerel
becquerel•3y ago
in most IDEs, you get that useful '1 usage' tag above properties, but not fields
becquerel
becquerel•3y ago
additionally, properties are required for a lot of frameworks that require attributes over methods -- such as MVVM frameworks and serialization
yotabit
yotabit•3y ago
I think that the easiest way to see the reason is to understand that not always you'll just get/set the same value. For example:
private int myVar;
public string MyVar
{
get => myVar.toString();
set => myVar;
}
private int myVar;
public string MyVar
{
get => myVar.toString();
set => myVar;
}
In this case, you return a String, but what you have is an integer
LDami
LDamiOP•3y ago
I know that, my question was about if I take the exact same value
yotabit
yotabit•3y ago
Yes. It's exactly the same value
LDami
LDamiOP•3y ago
I forgot about that point, you are right !
becquerel
becquerel•3y ago
dp, they mean in the degenerate case where it's the same type with public get/set
LDami
LDamiOP•3y ago
There can be difference in some methods calls between variable and properties ? šŸ¤”
Pobiega
Pobiega•3y ago
The most important part is "because now you can change it without breaking things later"
becquerel
becquerel•3y ago
thisowo a very good point
LDami
LDamiOP•3y ago
And is there any convention for when we must use variable instead of property ?
Pobiega
Pobiega•3y ago
The convention is always use properties for public things
becquerel
becquerel•3y ago
fields for private implementation details, properties for publicly-exposed stuff
Pobiega
Pobiega•3y ago
in normal C# at least. in unity, its different
becquerel
becquerel•3y ago
public class MyClass
{
private int _myField;
public int MyProperty { get; set; }
}
public class MyClass
{
private int _myField;
public int MyProperty { get; set; }
}
LDami
LDamiOP•3y ago
šŸ‘ šŸ‘Œ So 'get; set;' without associated private variable is good too ?
becquerel
becquerel•3y ago
yep, absolutely fine it's equivalent to writing it out the long way šŸ™‚ you only need the private field if you do more complex stuff
Aeon Vex
Aeon Vex•3y ago
I'd say in unity properties are still preferred, you just can't really use auto properties if you want to serialize
LDami
LDamiOP•3y ago
Like read-only Property ?
becquerel
becquerel•3y ago
nope, you can do public int MyProperty { get; } by itself just fine i mean like this
LDami
LDamiOP•3y ago
Oh I was thinking about private set; but it's the same, my bad
becquerel
becquerel•3y ago
private int myInt;

public MyInt
{
get => myInt * 2;
set
{
if (value % 2 == 0) myInt = value;
}
}
private int myInt;

public MyInt
{
get => myInt * 2;
set
{
if (value % 2 == 0) myInt = value;
}
}
contrived example, but yeah
FroH.LVT
FroH.LVT•3y ago
Invoke some event on set . Eg
public string Name {get {
return privateName;

}; set{
privateName = value;
InvokeSomeEventHere()
})
public string Name {get {
return privateName;

}; set{
privateName = value;
InvokeSomeEventHere()
})
LDami
LDamiOP•3y ago
Will this not trigger an infinite loop when setting Name ?
FroH.LVT
FroH.LVT•3y ago
ah yes, fixed
LDami
LDamiOP•3y ago
Okay, I unserstand ! Thanks šŸ™‚

Did you find this page helpful?