Simple usage of C # get Set

Source: Internet
Author: User

There are many articles about C # get set, but the author of this article has its special, the author uses the simple language to describe the C # get set is very clear.

C # Get set release one: The accessor of a property contains an executable statement about the get (read or evaluate) or set (write) property.

An accessor declaration can contain either a get accessor or a set accessor, or both. The declaration takes one of the following forms: The get {}set {} get accessor get accessor body is similar to the method body. It must return the value of the property type. Executing a get accessor is equivalent to reading the value of a field. The following is a get accessor that returns the value of the private field name:

    1. Private string name; //The Name field
    2. Public String Name //The name property
    3. {
    4. get { return name; }  
    5. }

When a property is referenced, the get accessor is called to read the value of the property unless the property is an assignment target. For example: Employee e1 = new Employee (), .... Console.Write (E1.   Name); The get accessor is invoked this get accessor must be terminated in a return or throw statement, and the control cannot exceed the accessor body. The set accessor set accessor is similar to the method that returns void. It uses an implicit argument called value, and the type of this parameter is the type of the property.

In the following example, the set accessor is added to the Name property:

    1. public string Name
    2. {
    3. get { return name; }    
    4. set {name = value; }  
    5. }

When you assign a value to a property, the set accessor is called with the parameter that provides the new value. For example: E1.   Name = "Joe"; The set accessor is invoked it is an error to use the implicit parameter name (value) for a local variable declaration in the set accessor.

C # Get Set Remarks:

Properties are categorized according to the accessors used: properties with only a get accessor are called read-only properties. Cannot assign a value to a read-only property. A property with only a set accessor is called a write-only property. A write-only property cannot be referenced except as the target of an assignment. Properties with both get and set accessors are read-write properties.

In a property declaration, both get and set accessors must be declared inside the property body. Using the get accessor to change the state of an object is a bad programming style. For example, the following accessors produce side effects that change the state of the object each time the number field is accessed.

    1. public int number
    2. {
    3. get { return number++; //Don ' t do this}
    4. }

You can use the get accessor to return a field value, or to calculate a field value and return it. For example:

    1. Public string Name
    2. get { return name = null? Name: "NA"; }  

In the preceding code snippet, if you do not assign a value to the Name property, it returns the value NA. Example 1 This example shows how to access a property that is hidden by another property in a derived class that has the same name in the base class.

  1. Property_hiding.cs
  2. Property Hidingusing System;
  3. Public class BaseClass
  4. {
  5. private string name;
  6. public string Name
  7. {
  8. get { return name; }    
  9. set {name = value; }   
  10. }
  11. }
  12. Public class Derivedclass:baseclass
  13. {
  14. private string name;
  15. public New string Name //Notice The use of the new modifier
  16. {
  17. get { return name; }      
  18. set {name = value; }    
  19. }
  20. }
  21. Public class MainClass
  22. {
  23. public static void Main ()
  24. {
  25. DerivedClass D1 = new DerivedClass ();
  26. D1. Name = "John"; //Derived class Property
  27. Console.WriteLine ("Name in the derived class is: {0}", D1.      Name);
  28. ((BaseClass) d1). Name = "Mary"; //Base class Property
  29. Console.WriteLine ("Name in the base class is: {0}", ((BaseClass) d1).     Name);
  30. }
  31. }

Output name in the derived class Is:johnname in the base class Is:mary the following is the focus shown in the previous example: a property in a derived class name hides the property name in the base class. In this case, the property declaration of the derived class uses the new modifier: public new string Name {... The transform (BaseClass) is used to access the hidden property in the base class: ((BaseClass) d1). Name = "Mary";

C # Get Set release two: The Code is as follows:

    1. public class car  
    2.     private  string color;   
    3.     public string color   
    4.     {    
    5.            get   { return color;    }    
    6.           set     {color=value;    }  
    7.    }  
    8. Li class= "alt" >  

My understanding is that the read and write operation of the public variable color through get and set is actually the indirect change of the value of the color private variable. Why not set the color to public and let the instance read and write to color directly?  If one day, the boss let you change this class to when the color of the car changes, while calculating the car's "price" property then if the color operation directly, you are not dead? "Attributes" is one of the features of. Net.

In fact, the equivalent of methods, especially in Java often use GET, set method (some of the ideas of. NET is Java).  The real effect of a property is not just to change the value of a member variable such as the Size property of a form to redraw a form while set, if you do not want the user to modify the color, do not provide the C # Get Set method is object-oriented with the set and get its purpose: In general, the variables inside the class are manipulated. Instead of directly manipulating the variables of the class.

One of the big things is that it's easy to maintain. Because: if a variable int a of a class is used 1000 times in another package or namespace class, but after a long time, you want to change A to B, and if you work directly on variable A, you need to modify 1000 of the entire program. If the property is used, it will not, just change this method can be public int a{set {a = value;} get {return A;}} Put as: public int b{set {B = value;} get {return B;}} Except for this attribute, there is no need to change the place.

Through the above explanation. A little bit clear. is not to let get and set to meet certain conditions to change the private variables in the class. Instead of allowing the instance to be manipulated directly. The code like the above guarantees the security of the color property.  If so can write set{color=value*20; Value is not equivalent to color} I had the same idea as you. But now it's changed. Give me an example to illustrate it.

  1. Public class Car
  2. {
  3. public string Color
  4. {
  5. get {
  6. if (this.viewstate["COLOR"]!= null)
  7. {
  8. return this.viewstate["Color"];
  9. }
  10. return "":
  11. }
  12. set { this.viewstate["Color"];=value; }   
  13. }
  14. }

in ASP. NET is usually used in this way. If you use a variable, it's not good to use. And you can write multiple statements in C # get set.

Original address:

Simple usage of C # get Set

Related Article

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: 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.