What is the purpose of the get and set properties in C #

I saw some set a method for setting values. Can someone tell me the purpose of this?

public string HTTP_USER_NAME { get { return UserName; } set { UserName = value; } } public string HTTP_USER_PASSWORD { get { return UserPwd; } set { UserPwd = value; } } 

In fact, why use these things. For global access, or is there some other reason for this kind of thing?

+7
source share
7 answers

These are just accessors and mutators. How properties are implemented in C #

In C # 3, you can use automatically implemented properties, for example:

 public int MyProperty { get; set; } 

This code is automatically translated by the compiler into code similar to the one you published, with this code it is easier to declare properties, and they are ideal if you do not want to implement your own logic inside set or get , you can use a different access method for the set method, making the property unchanged

 public int MyProperty { get; private set; } 

In the previous example, MyProperty will be read only outside the class where it was declared, the only way to mutate it is to expose the method for this, or simply through the class constructor. This is useful when you want to control and make explicit the state change of your entity.

If you want to add some logic to the properties, then you need to write properties that manually implement the get and set methods in the same way as you placed:

User logic implementation example

 private int myProperty; public int MyProperty { get { return this.myProperty; } set { if(this.myProperty <=5) throw new ArgumentOutOfRangeException("bad user"); this.myProperty = value; } } 
+16
source

It seems that you understand the functionality of getters and setters, while others answered this question. The "normal" class variables (without getters and setters) are called "fields", and the "properties" (which getters and setters have) encapsulate the fields.

The purpose of the properties is to control external access to the fields. If you want the variable to be read-only for external logic, you can omit setters, for example:

 private int dataID; public int DataID { get { return dataID; } } 

You can also make setter private and get the same read-only features.

If an object is likely to be null (for any reason), you can guarantee that the instance always exists as follows:

 private Object instance; public Object Instance { get { if (instance == null) instance = new Object(); return instance; } } 

Another use of properties is the definition of indexers.

 //in class named DataSet private List<int> members; public int this[int index] { get { return members[index]; } } 

Given this indexer, you can access the DataSet instance as follows:

 int member = dataSet[3]; 
+10
source

Check out these links, .. they give a clear explanation.

http://www.dotnetperls.com/property

http://code.anjanesh.net/2008/02/property-getters-setters.html

if UserName and UserPwd are class variables, it is better to use this

 _userName _userPwd 
+4
source

The standard way to implement properties in C # . UserName and UserPwd are private member variables ( string type) of the class in which these 2 methods are defined.

+2
source

HTTP_USER_NAME and HTTP_USER_PASSWORD are the public properties of your class. UserName and UserPwd can be your private field. And you allow other people to set or receive values ​​through these public properties. There is no direct access to private benefits. You can also do some logic inside the get method of a property. Ex: you will have a public property called Age , and in the get method you can read the value of your private field called " dateOfBirth " and do some calculations (CurrentYear-dateOfBirth) and return them as Age.

+2
source

Properties are just field accessors. They allow you to perform certain operations (if necessary) and provide controlled access to the fields.

If you want to know when to use properties and when to use fields only, check the link "Properties versus fields" - why does it matter? (Jonathan Anecha)

+2
source

From Properties (C # Programming Guide)

A property is a member that provides a flexible mechanism for reading, writing, or calculating the value of a private field. Properties can be used as if they were public data elements, but in reality these are special methods called accessors. This makes it easy to access data and still helps increase the safety and flexibility of the methods.

In this example, the TimePeriod class stores the time period. Inside the class, time is stored in seconds, but a property called Hours allows the client to specify the time in hours. Accessors for the clock property convert between hours and seconds.

Example

 class TimePeriod { private double seconds; public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } } } class Program { static void Main() { TimePeriod t = new TimePeriod(); // Assigning the Hours property causes the 'set' accessor to be called. t.Hours = 24; // Evaluating the Hours property causes the 'get' accessor to be called. System.Console.WriteLine("Time in hours: " + t.Hours); } } // Output: Time in hours: 24 

Property Overview

  • Properties allow the class to expose a publicly accessible way of getting and setting values, hiding the implementation or verification code.

  • A get property accessor is used to return the value of the property and set accessor is used to assign a new value. These accessors can have different access levels. For more information, see Accessor Accessibility Limitations (C # Programming Guide) .

  • value is used to determine the value that is assigned by the set .. p>

  • Properties that do not implement the set accessory are read-only.

  • For simple properties that do not require a special access code, consider using automatically implemented properties. For more information, see Automatically Implemented Properties (C # Programming Guide) .

+1
source

All Articles