When and why to use C # access methods

Possible duplicate:
C # - When to use properties instead of functions

I am trying to understand when and why to use "getters" and "seters"

Someone please give some recommendations.

What is the difference between the following constructs - look only at access methods.

//EXAMPLE 1: simple accessor method private static bool _isInitialEditMapPageLoad; public static bool isInitialEditMapPageLoad { get {return _isInitialEditMapPageLoad;} set {_isInitialEditMapPageLoad = value;} } //EXAMPLE 2: accessor method with a conditional test private static bool _isInitialEditMapPageLoad; public static bool isInitialEditMapPageLoad { get { if (currentSession[isAuthorizedUseder] == null) return false; else return _isInitialEditMapPageLoad; } set {isInitialEditMapPageLoad = value;} } //EXAMPLE 3: just a get accessor method - is this the same as EXAMPLE 4? private static bool _isInitialEditMapPageLoad = false; public static bool isInitialEditMapPageLoad { get {return _isInitialEditMapPageLoad;} } //EXAMPLE 4: simple method private static bool _isInitialEditMapPageLoad = false; public static bool isInitialEditMapPageLoad { return _isInitialEditMapPageLoad; } 
+6
c # accessor
source share
4 answers

1: This is a simple property and can be used in much the same way as in a public field. If you have a reason to show get and set operations to other users (i.e. other classes), and you don't need anything interesting, that’s all. This can also be written using "auto-properties",

 public static bool isInitialEditMapPageLoad {get;set;} // behaves just like example 1 

automatic details are written much faster and, in my opinion, much more readable than a full declaration (if I see a full declaration using the support field, I expect to find some complexity).

2: This shows one of the reasons for the properties: using some logic to return the value, rather than return the value directly. Someone can set this value as they will be a public field whenever they want. They can get the value whenever they want, with the proviso that false means that this is not a boot, or the user is not authorized, that is, some (simple) logic is executed before the value is returned.

3: This behaves like a public field for READ ONLY - someone can get the value but not set it. This is essentially a value that is read only by external code (not to be confused with the readonly keyword)

4: Result for compilation for me. Assuming this should be a method declaration, manually defining a getter, as in Java, it looks like Example 3. I believe that there are other problems that make this not quite so, for example, if you want to turn this into a dependency property and etc. Unfortunately, my knowledge in this area is declining.

===========

Typically, user properties restrict access to your class data. As a principle, everything that you can hold by allowing another code to touch should be saved this way. In practical terms, you will want to set the values ​​in the classes to change the display method, change the data presented, etc. Use properties to maintain maximum control over this interaction.

If other classes need to look at something in their class, you will need to open the getter, but not the setter. This is not possible with fields unless you use the Java method to create a custom getter method. They also allow you to perform calculations or checks before returning or adjusting data. For example, if you have an integer value that must be within a certain range (a range that may vary depending on the state of your object, even), you can check in your setter to make sure that this condition is met before your actual update value.

Try to avoid the trap by simply installing everything as an auto program - this is no different from making everything publicly available. Keep everything as private as possible. Without getters, if it is not necessary, there are no setters if it is necessary, and setters must carry out any small logic necessary to verify the input before accepting it if necessary. However, avoid another trap: put a lot of code in getters / setters. If you need more than a few lines, you should create a method rather than a property, simply because it gives a bigger hint to others, using your code, that something big will happen.

+3
source share

Your recipients / setters should be your open interface for your class.

As a rule, all your members should be private, except that you want people to have access to outside your class, and you never want your private variables to be accessible directly outside if your class

Here is a simple example. let's say you had a class for which you need the age variable. In this case, you can perform validation directly in the setter if your outer classes do not need to know that this value has been verified.

 class Person { int age = 0; public int Age { get { return age; } set { //do validation if (valid) { age = value; } //Error conditions if you want them. } } //More getters/setters } 
+8
source share

The rationale behind Getters / Setters is to protect the class from being violated when the user modifies the field in an invalid way, and they allow you to modify the implementation of your class, leaving unchanged publicly available properties.

If you don't need any checks or lazy properties, you can usually just use automatic properties.

 public string Name { get; set; } 
+5
source share

Like the others mentioned, use getters / seters when you want the members of an object to be accessible to other objects.

In addition, yoru code readability can be improved (if you are using .NET 2.0 or higher) using autoproperties. The following are examples:

 // example 1 public static bool IsInitialEditMapPageLoad { get; set; } // example 3/4 - note that false is the default for bools public static bool IsInitialEditMapPageLoad { get; private set; } 

Example 3 is likely to remain the same, due to the validation logic.

+2
source share

All Articles