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 šŸ™‚
Want results from more Discord servers?
Add your server