C # in real property

introduction

We said before the class class among the "Properties", in fact, the official name for the "field" or "field" domain (Field). Normal use, when the attribute them to understand more convenient, there is no problem.

If you want to look at the difference between formal and necessary link between the two of them take advantage of the convenience brought about in class Microsoft mvc, there is.

Introduction

On msdn is such a description of the property:

Property is a member that provides a flexible mechanism to read, write or calculated values ​​of the private field. Properties can be used as public data members, but they are actually special methods called accessors. This makes it easy to access the data, but also help improve the security and flexibility of the method.

Properties Overview
Properties public class disclosed method allows to get and set values, it hides or validate the code.
get access attribute property value for return, and the set property accessors for assigning a new value. These visits can have different access levels.
value for the key value is assigned by the access definition set.
Properties can be read - write properties (both get accessor have set accessor), read-only attribute (accessor get there, but there is no set accessor) or write-only accessor (have a set accessor, but did not get access device).

There is a basic pattern properties, which uses a private field support to set and retrieve attribute values. The get accessor returns the value of a private field, set accessor before assigning to the private field may perform some data validation. The two accessors may also perform some of its calculation or conversion prior to storage or return data.

Example: There is a class called Student, with private fields age, name and code (ie we have said before the property is set to private access, hereinafter called fields). We can not directly access these fields outside the scope of the class, but we can have access to these private property domain.

class Student 
    { 
        Private  string code = " NA " ;
         Private  string name = " Not Known " ;
         Private  int Age = 0 ; 

        // Code attribute type string declared 
        public  string Code 
        { 
            GET 
            { 
                return code; 
            } 
            SET 
            { 
                code = value ; 
            } 
        } 
        // Age attribute declared type int
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if (value > 200)
                {
                    age = 200;
                }
                else
                {
                    age = value;
                }
            }
        }
        public override string ToString()
        {
            return "Code = " + Code + ", Name = " + + Name " , Age = " + Age; 
        } 
        // declare a type string Name attribute 
        public  string Name 
        { 
            GET 
            { 
                return name; 
            } 
            SET 
            { 
                name = value; 
            } 
        } 
    } 
        class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            // create a new Student object 
            Student = S new new Student();

            // 设置 student 的 code、name 和 age
            s.Code = "001";
            s.Name = "Zara";
            s.Age = 210;
            Console.WriteLine("Student Info: {0}", s);
            // 增加年龄
            s.Age -= 100;
            Console.WriteLine("Student Info: {0}", s);
            Console.ReadKey();
        }
    }

Results of the:

Example 2:

In this example, TimePeriod class represents the time interval. Internally, such time interval in seconds is stored in the private field named _seconds. Entitled Hours of reading - write property allows customers in hours specified time interval. Will perform the necessary conversion between hours and seconds get and set accessor. In addition, set accessor will verify the data, if the number of hours is not valid, throws an ArgumentOutOfRangeException.

class TimePeriod
{
   private double _seconds;

   public double Hours
   {
       get { return _seconds / 3600; }
       set { 
          if (value < 0 || value > 24)
             throw new ArgumentOutOfRangeException(
                   $"{nameof(value)} must be between 0 and 24.");

          _seconds = value * 3600; 
       }
   }
}

class Program
{
   static void Main()
   {
       TimePeriod t = new TimePeriod();
       // The property assignment causes the 'set' accessor to be called.
       t.Hours = 24;

       // Retrieving the property causes the 'get' accessor to be called.
       Console.WriteLine($"Time in hours: {t.Hours}");
   }
}

Automatically properties
under certain circumstances, get and set attributes assigned only accessor to retrieve wherein only supports fields or values, it does not include any additional logic.
Auto-implemented properties defined in the following ways: Use the get and set keywords, but does not provide any implementation.

Example:

public class SaleItem
{
   public string Name 
   { get; set; }

   public decimal Price
   { get; set; }
}

class Program
{
   static void Main(string[] args)
   {
      var item = new SaleItem{ Name = "Shoes", Price = 19.95m };
      Console.WriteLine($"{item.Name}: sells for {item.Price:C2}");
   }
}

 

Guess you like

Origin www.cnblogs.com/wanjinliu/p/11657284.html