C # -- set and get

Source: Internet
Author: User

In Object-Oriented Programming (OOP), it is required that the external parties not be allowed to directly access the member variables of the class. Since these member variables cannot be accessed, what is the significance of defining these member variables? Therefore, in C #, we need to use the set and get methods to access private member variables. They are equivalent to a channel and an "interface" for external access objects ". Let's take a look at a piece of code:

 class Employee    {        private string name;        private byte age;        public string Name        {            get { return name; }            set { name = value; }        }        public byte Age        {            get { return age; }            set { age = value; }        }    }
The Code defines two private variables: name and age. When we do not want the outside world to access the private variable at will, we can use the attribute to access the variable. Syntax:

Public <return type (to be the same as the type of the accessed variable)> <attribute name (cannot be the same as the accessed variable)> {get {return <accessed variable> ;} set {<accessed variable >=value ;}}
When we use properties to access private member variables, we will call the get method. When we want to modify this variable, we will call the set method, of course, you can define only one get method or only one set method. If only the get method is defined, the corresponding variable is "read-only". If only the set method is defined, the corresponding variable is "Write-only.


At this point, I have a question. Since the external world can use private members in the set and get requests classes, why don't we define them as a common and direct access? Take the preceding Employee class as an example:

Class Employee {private string name; private byte age; public string Name {get {return name ;}set {name = value ;}} // ***** after modification, ***** please specify your own temporary public byte Age {get {return age ;} set {if (value> 10 & value <= 100) // generally, employees in the company are between the ages of 10 and 100 age = value ;}} // ***** after modification}
I believe that this example can be very clear. In this example, set is like an uncle guard. Only good people can come in. You can use attributes ControlReads and writes member variables to prevent illegal assignment of member variables.

A few days ago, at the Academic Exchange conference, I just talked about customer changes. They have a small role in this regard. Let's take a small example:

There is a thermometer class:

Class Thermometer {private double temperature; public Thermometer (double temperature) // constructor {this. temperature = temperature;} public double Temperature {get {return temperature;} set {temperature = value ;}}}

Assume that the temperature unit here indicates the degree Celsius (℃). If you misunderstand the customer's original intention or the customer changes the requirement in the demand analysis phase, all the temperature variables in the system, temperature, must be used to represent the kervin temperature (K ). You do not need to change the hundreds of Temperature attributes in the system. You only need to modify the code in the get and set functions:

Class Thermometer {private double temperature; public Thermometer (double temperature) // constructor {this. temperature = temperature;} public double Temperature {// ***** after modification, ***** too many temporary get {return temperature-273.15 ;} set {temperature = value + 273.15;} // ***** after modification }}
The following is a simple console application to test. The main function is:

 class Test    {        static void Main(string[] args)        {            Thermometer a = new Thermometer(40);            Console.WriteLine(a.Temperature);        }    }
The initial value of the temperature is set to 40 degrees using the constructor, so the running result before the code modification is:


After the code is modified, the running result is:



If you have any shortcomings or omissions, please leave your valuable comments and suggestions. Thank you very much!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.