Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using {set; get;} instead of {get; set;}

Tags:

c#

properties

oop

In C# and its cousin languages, we always use

public string SomeString { get; set;}

But you can also use ( I found this out only recently and while fooling around with the compiler )

public string SomeString { set; get; }

I do not have any formal training in programming and everything is self-tought. I have been using { get; set; } without any thought just like we use 1 + 1 = 2 Is the order of { get; set; } just a convention or is it necessary to maintain this order or is it some remnant of a bygone era of C history like the way we define conventional electric current flowing from positive to the negative terminal when it is actually the other way around?

like image 628
Flood Gravemind Avatar asked Oct 14 '13 11:10

Flood Gravemind


People also ask

Can you use set without get?

you can remove get and set it will not affect the code and working due to the reason that you have defined a variable of type int with the Access type of public so that properties are mostly used to access the private members of class which is in your case do not exist so go on and remove it how ever if in top most ...

Why use get set instead of public?

The main difference between making a field public vs. exposing it through getters/setters is holding control over the property. If you make a field public, it means you provide direct access to the caller. Then, the caller can do anything with your field, either knowingly or unknowingly.

Why use Get set property in C#?

In c#, properties can contain one or two code blocks called accessors, and those are called a get accessor and set accessor. By using get and set accessors, we can change the internal implementation of class variables and expose it without affecting the external way of accessing it based on our requirements.

What is the Get set syntax in C#?

The { get; set; } syntax in C# is a shorthand for the automatic property. The only difference is that with automatic properties we cannot access the private variable.


1 Answers

It is purely a convention. It makes no difference which order they appear in.

like image 133
David Arno Avatar answered Oct 06 '22 00:10

David Arno