C
C#8mo ago
Carl

✅ About get; set properties

Hi, I have a question about get/set properties. Is that using get; set; properties always better without using them? Is there any affect if I use all my properties with them?
2 Replies
Kiel
Kiel8mo ago
you're talking about the difference between
public int SomeValue;
public int SomeValue;
and
public int SomeValue { get; set; }
public int SomeValue { get; set; }
? From my experience, the second is always preferred nowadays. public fields have a lot less control over accessibility and visibility, specifically when it comes to setting and getting the value. properties (the second example) are the more modern and recommended way to do things, EVEN IF you are just doing get; set; for everything. properties in general are just much more flexible. You can do cool things like have a property with a backing field that you can do (simple) data validation on as the property is set, for example:
private int _someValue;

public int SomeValue
{
get => _someValue;
set
{
if (value == 42)
throw new MeaningOfLifeException();

_someValue = value;
}
}
private int _someValue;

public int SomeValue
{
get => _someValue;
set
{
if (value == 42)
throw new MeaningOfLifeException();

_someValue = value;
}
}
Another usecase for properties is limiting other classes' ability to read/write to the property as needed.
public int SomeValue { get; private set; }
public int SomeValue { get; private set; }
Any other class can read SomeValue, but only the class it's in can modify its value. IMO, it's better to just use properties everywhere* for consistency. *fields are still OK for private variables you need shared within a class
Carl
Carl8mo ago
Yes, that's my question about. Thank you for your answer, you have 2 more examples that I can see how better it is
Want results from more Discord servers?
Add your server
More Posts
✅ entity framework return all objectsHello, If I use entity framework and I want to get all objects in my repository, what should I do? ❔ Azure Linux Functions, dotnet isolatedI have an issue with my Azure Linux functions, they are made like this in terraform: https://pasteb❔ Working with string builderI wonder if there is a way that I can avoid passing the string to be formatted in the AppendFormat m❔ Is my Unity + VS setup correctly? (NEWBIE HELP:C)I am NEW and I mean NEW to the coding world as well as computers and I was interested in making a ga❔ factorial using equationSo i am stuck on this task where i have to also use equation for example 1*1 =1 1*2=2 2*3=6 and i sWeird behaviour if I included code after File.ExistsSo the EvaluateDbFile function is checking if a db file exists, if I leave it as it is now it evalua❔ Big Problem in C# plz someone helpso i'm new to coding (just so you know so if you say something to advanced i will maybe not know wha❔ How do I name an object after a variable in that object?I have a class with a variable called "name", how would i make "name" the name of the object?❔ Hi for some reason this exercise on indexing isnt clicking for me can someone guide me this?1-im used to properties that have a name this property has no name so i guess it refers to the class❔ Limit incremental generator's RegisterPostInitializationOutput() call to one specific assemblyIs it possible to add an attribute declaration code to a single assembly and let the code generation