Set and Get methods in C #

Source: Internet
Author: User

In object-oriented programming (OOP), it is not allowed to directly access the member variables of the class, since it is inaccessible, what is the meaning of defining these member variables? So in C #, you use the set and get methods to access private member variables, which are equivalent to a single channel of the outside Access object, an "interface." Take a look at the code first:

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, and when we don't want the private variables to be accessible to the outside world, we can use properties to access the syntax:

Public < return type (to be the same type as the variable being accessed) > < property name (cannot be the same name as the accessed variable) >
{
get{return < accessed variable;;}
set{< accessed variable > = value;}
}

When we use a property to access a private member variable, we call the Get method inside, and when we want to modify the variable we call the Set method, which, of course, can define only a Get method or a set method. If only the Get method is defined, the corresponding variable is "read-only", and if only the set method is defined, then the corresponding variable is "write-only".

Since the outside world can access the private members of the class through set and get, why not directly define it as a common, directly accessible to the outside world? Take the employee class above to illustrate:

Class Employee
{
private string name;
private byte age;
public string Name
{
get {return name;}
set {name = value;}
}
****↓↓↓↓↓↓↓↓ after modification
Public byte Age
{
get {return age;}
Set
{
if (Value > && value<=100)//General employees in the company are aged between 10-100 years old
Age = value;
}
}
****↑↑↑↑↑↑↑↑ after modification
}

In the example above, set is like a janitor's uncle, only a good person can come in. Properties can be used to control the reading and writing of the member variable, to prevent illegal assignment of membership variables, and so on.

For a small example, here 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;}
}
}

Assuming that the temperature units here represent degrees Celsius (℃), if the customer's original intention is misunderstood during the demand analysis phase or if the customer changes the demand in the future, all variable temperature representing the temperature in the system must be used to represent the Kelvin temperature (K). So you don't have to move hundreds or thousands of temperature properties in the system, just slightly 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
get {return temperature-273.15;}
set {temperature = value+273.15;}
****↑↑↑↑↑↑↑↑ after modification
}
}

Build a 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 above code with the constructor to set the temperature of the initial value of 40 degrees, the code before the change to run the result of "40", the code after the result of the change is "233.15".

Article reprinted from: http://blog.csdn.net/u013095889/article/details/52804360

Set and Get methods in C #

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.