Jump to content

[C#] Properties question

Go to solution Solved by madknight3,

Simply use the automatic property whenever you don't add any more code. If you do, use the long version. When you do use the long version because you're adding in more code, the getter should be kept simple and shouldn't throw exceptions, while the setter can (source).

 

 

Also, if anyone wonders what the difference is between an automatic property and a simple public field

public int foo { get; set; }public int bar;

then see this answer.

Hi all. I'm a tad confused right now. I've been using properties in C# for a while now, and I want to ask very quickly if there is any real difference between this

public string filePath { get; set; }public sting name { get; set; }

And this

private string filePath;private string name;public string FilePath{    get {return filePath;}    set {filePath = value;}}public string Name{    get {return name;}    set {name = value;}}

I am massively confused right now, and I might even be thinking too far into things. I decided if anyone could answer, the LTT community can. I've tried googling it, but most of what I found were just comparisons between methods and properties. I believe both of these types are properties

Link to comment
https://linustechtips.com/topic/270529-c-properties-question/
Share on other sites

Link to post
Share on other sites

well i dont know what its called in english but the ones below are public properties that only reference the private once.

that way you can do stuff as getter or setter. simple formulas fo example or stuff like that

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
https://linustechtips.com/topic/270529-c-properties-question/#findComment-3670882
Share on other sites

Link to post
Share on other sites

No, they are equivalent. Automatic properties are there for convenience.

 

With that said, obviously the properties written out fully can do more.

 

 

well i dont know what its called in english but the ones below are public properties that only reference the private once.

that way you can do stuff as getter or setter. simple formulas fo example or stuff like that

So they are basically the same thing at the core, just the second ones are able to implement more to them, such as data validation?

Link to comment
https://linustechtips.com/topic/270529-c-properties-question/#findComment-3670934
Share on other sites

Link to post
Share on other sites

So they are basically the same thing at the core, just the second ones are able to implement more to them, such as data validation?

yes basicly, i usually use the second once because of that. if you think hard enough almost every propertie has some code to validate it or something. the more you can put in setters and getter the less you have to do somewhere else in the code it saves a lot of code and dokumentation 

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
https://linustechtips.com/topic/270529-c-properties-question/#findComment-3671255
Share on other sites

Link to post
Share on other sites

Simply use the automatic property whenever you don't add any more code. If you do, use the long version. When you do use the long version because you're adding in more code, the getter should be kept simple and shouldn't throw exceptions, while the setter can (source).

 

 

Also, if anyone wonders what the difference is between an automatic property and a simple public field

public int foo { get; set; }public int bar;

then see this answer.

Link to comment
https://linustechtips.com/topic/270529-c-properties-question/#findComment-3673380
Share on other sites

Link to post
Share on other sites

Simply use the automatic property whenever you don't add any more code. If you do, use the long version. When you do use the long version because you're adding in more code, the getter should be kept simple and shouldn't throw exceptions, while the setter can (source).

 

 

Also, if anyone wonders what the difference is between an automatic property and a simple public field

public int foo { get; set; }public int bar;
then see this answer.
so if you're just going to be using basic fields with no need for validation or extra code, it's best to use the automatic properties?
Link to comment
https://linustechtips.com/topic/270529-c-properties-question/#findComment-3675644
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×