{ get; set;} and access modifiers

I just started learning C #, and I'm struggling a bit with getter and setter abbreviations.

As I understand it, the two methods below are equivalent. Is it correct?

//Method 1 public string name { get; set; } //Method 2 private string name public string getName() { return name;} public string setName(string newName) { this.name = newName; } 

Secondly, how does it work if we want to use different access modifiers in getter / setter and instance variable. The following code errors telling me that an accessory should be more restrictive than a property, and that I cannot specify modifiers for auxiliary devices. Can someone clarify?

 private int maxTime { public get; public set; } 

EDIT: To clarify, I don't have a specific goal, just to understand. I don’t understand what short notation is. In other languages, I had private instance variables and used public getters and setters to manage these instance variables. This allows this if I write out the methods myself, but not with this abbreviation. Why is this?

EDIT2: Last question to test my understanding. Both of the code snippets below use properties to control the maxTime variable. The only difference between them is style. Is it correct?

 private int maxTime; public int MaxTime{ get; set; } 

against

 private int maxTime; public int MaxTime { get { return maxTime; } set { maxTime= value; } } 
+7
c #
source share
5 answers

Instead of the wrong private int maxTime { public get; public set; } private int maxTime { public get; public set; } private int maxTime { public get; public set; } , you can write a property that will fill the private field:

 private int maxTime; public int MaxTime { get { return maxTime; } set { maxTime = value; } } 

This is useful when you want to apply logic when getting or setting the maxTime value. if not, then a simple shorthand property will do:

 public int MaxTime { get; set; } 

You can create a property that has a public recipient but a private setter, for example:

 public int MaxTime { get; private set; } 

This is useful for readonly properties, and usually the property is populated in the constructor of the class.

You can even create a property in which the setter is public, but getter is private, although I can’t imagine which scenario would be useful. Moreover, code standards state that such a thing should be a method, not property. (read it)

 public int MaxTime { private get; set; } 

Revision 2 does not answer your question.

the first code never changes the private int maxTime , and the second -. However, if you only use the maxTime property inside your class, then they are functionally equivalent.

Update:

Since C # 6 you can write shorthand properties without a setter:

 public int MaxTime {get;} 

These properties can only be initialized in the constructor or hardcoded as follows: (also a new C # 6 function)

 public int MaxTime {get;} = DateTime.Now; 

This is useful for immutable properties (unlike readonly properties, the value of such a property cannot change even inside the hosting class after initialization.

+13
source share
 //Method 1 public string name { get; set; } //Method 2 public string name public string getName() { return name;} public string setName(string newName) { this.name = newName; } 

The above 2 methods are not equivalent.

It would be more accurate to compare them as follows:

 //Method 1 public string name { get; set; } //Method 2 private string name; // this is private, not public. public string Name // this is a property, not a method. { get { return this.name; } set { this.name = value; } } 

And if you want to play with access modifiers, for example, make public get public and set private, then you will do it like this:

 public int maxTime { get; private set; } 

Additional Information About Authorized Properties and Compiler Magic, Which Continues Behind the Scenes.

+6
source share
 public string name { get; set; } 

You have an "Authorized Property" in which the private field will be internally supported (and compile time methods for get / set).

In C # code, this would be equivalent to:

 private string _name; public string name { get { return _name; } set { _name = value; } } 

At compile time, get / set is converted to method calls, somewhat similar to what you have.

For:

 private int maxTime { public get; public set; } 

The error is pretty clear, you cannot have less restrictive access specifications than the property itself. For example, if you want your public property to have public get , but if you want to allow setting the property inside the class, you can do it:

 public int maxTime { get; private set; } 

You should also see:. Net Use Agreements . It would be better if you follow this so that you can have your property name starting with a capital letter.

+3
source share

The first method is just C # syntactic sugar for an automatically implemented property. It provides the implementation of appropriate accessories when you come to compile it.

The second example is different. Here you have a public-scope field (usually this is no-no due to the principle of encapsulation) and two methods that access the variable. There is a subtle difference in semantic use; usually, properties are used to reveal state, while a method usually indicates that a method has some calculations behind it and is not only going to return or change the current state (again, this is an agreement, not a hard and fast rule) Usually methods are called VerbAction (public Thing GetAThing () {}).

Auto-generated properties can have different access modifiers, but should only make get or set less accessible than the general modifier.

 public int X { get; private set; } // OK public int X { private get; set; } // OK private int X { public get; public set; } // NOT OK 
+2
source share

When you use a property similar to method 1 (public string name {get; set;}), the compiler automatically generates a string variable of the string type and the public methods getter and setter.

The general idea is to make the fields private and only allow access through the public getter / setter methods. Therefore, if you use method 2, declare a private variable.

I would recommend using ILDASM to view the created IL, this helps to understand what is happening under the hood.

The second error is just what the compiler says. The visibility of structures must be consistent.

+1
source share

All Articles