C
C#ā€¢2y 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ā€¢2y ago
in most IDEs, you get that useful '1 usage' tag above properties, but not fields
Becquerel
Becquerelā€¢2y ago
additionally, properties are required for a lot of frameworks that require attributes over methods -- such as MVVM frameworks and serialization
yotabit
yotabitā€¢2y 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
LDamiā€¢2y ago
I know that, my question was about if I take the exact same value
yotabit
yotabitā€¢2y ago
Yes. It's exactly the same value
LDami
LDamiā€¢2y ago
I forgot about that point, you are right !
Becquerel
Becquerelā€¢2y ago
dp, they mean in the degenerate case where it's the same type with public get/set
LDami
LDamiā€¢2y ago
There can be difference in some methods calls between variable and properties ? šŸ¤”
Pobiega
Pobiegaā€¢2y ago
The most important part is "because now you can change it without breaking things later"
Becquerel
Becquerelā€¢2y ago
thisowo a very good point
LDami
LDamiā€¢2y ago
And is there any convention for when we must use variable instead of property ?
Pobiega
Pobiegaā€¢2y ago
The convention is always use properties for public things
Becquerel
Becquerelā€¢2y ago
fields for private implementation details, properties for publicly-exposed stuff
Pobiega
Pobiegaā€¢2y ago
in normal C# at least. in unity, its different
Becquerel
Becquerelā€¢2y ago
public class MyClass
{
private int _myField;
public int MyProperty { get; set; }
}
public class MyClass
{
private int _myField;
public int MyProperty { get; set; }
}
LDami
LDamiā€¢2y ago
šŸ‘ šŸ‘Œ So 'get; set;' without associated private variable is good too ?
Becquerel
Becquerelā€¢2y 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ā€¢2y ago
I'd say in unity properties are still preferred, you just can't really use auto properties if you want to serialize
LDami
LDamiā€¢2y ago
Like read-only Property ?
Becquerel
Becquerelā€¢2y ago
nope, you can do public int MyProperty { get; } by itself just fine i mean like this
LDami
LDamiā€¢2y ago
Oh I was thinking about private set; but it's the same, my bad
Becquerel
Becquerelā€¢2y 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ā€¢2y 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
LDamiā€¢2y ago
Will this not trigger an infinite loop when setting Name ?
FroH.LVT
FroH.LVTā€¢2y ago
ah yes, fixed
LDami
LDamiā€¢2y ago
Okay, I unserstand ! Thanks šŸ™‚
Want results from more Discord servers?
Add your server
More Posts
How add analyzer for suggesting to add missing parameters or removing them?Soo I have analyzer which checks if parameter type is proper soo then I need to make codefix which j[ASP.NET] B-Logic Services, ValidationI am using anemic models, therefore I want to implement Business Logic Services. And the questions Optimizing some string manipulationI want to both substring an input string at the last occurrence of `'/'` and normalize it into *onlyRight way to use Interfaces in Large Scale Blazor Server Web ApplicationsHello there, iam currently working on a <see title> app for something iam not allowed to leak now iaUsing dependency injection for internal classes inside a libraryI'm making a library, where I feel like it would be useful to be able to use Microsoft's dependency System.InvalidOperationException The operation is not allowed on non-connected socketsi have client and server, client creates and connects tcp client to server tcp listener, client tcp HttpClient only working in Main but not in another Class? [Answered]I'm currently following a c# json tutorial and trying to put this code into it's own class so I can Which workload for blazor desktop?In VS2022, which workload(s) do I need to install to create a blazor desktop app?Style dependent on a propertyA Button `ConfirmationButton` should have the `Style= MahApps.Styles.Button.Dialogs.Accent` when theBranching and release strategy for multi-project repositoryI am currently working on improving the branching and release strategy for one of my projects. At th