c # property (Property)

Property (Property) is a class (class), the structure (Structure) and an interface (interface) named (named) members. Class or structure member variable or a method called  field (Field,) . Property (Property) is an extension field (Field), and may be accessed using the same syntax. They use  accessor (accessors)  so that the value of private domains may be read or operations.

The code is mainly to help the reader understand the usage of the property, code implements value adding attributes and attribute values ​​based on screening added

the using the System;
 the using the System.Collections;
 the using the System.Collections.Generic;
 the using the System.Globalization;
 the using the System.Linq;
 the using the System.Text; 

namespace encoding exercise 
{ 
    // Create a class contains two attributes 
    class People 
    { 
        public  String the Name { GET ; SET ;}
         public  String the address { GET ; SET ;}
         public People ( String name, String address) 
        {
            the this .Name = name;
             the this .Address = address; 
        } 

    } 
    // inherited two interfaces (enumeration / formatting) 
    class Peoples: the IEnumerable 
    { 
        // Create a list of objects 
        Private List <People> Lpeoples { GET ; SET ;}
         Private the StringBuilder Sbuilder { GET ; SET ;}
         public Peoples () 
        { 
            Lpeoples = new new list <People> (); 
        } 
        // Create a list of methods can be applied to the attribute instance of 
        public void Add(People people)
        {
            Lpeoples.Add(people);
        }
        //获取对象值
        public IEnumerator GetEnumerator()
        {
            foreach (var p in Lpeoples)
            {
                yield return p;
            }
        }

        public override string ToString()
        {
            return GetContent(Lpeoples);
        }
        public string Tostring(string format)
        {
            return ToString(format, CultureInfo.CreateSpecificCulture("zh-CN"));
        }
        public string ToString(string format, IFormatProvider formatProvider)
        {
            IEnumerable<People> ps = Lpeoples;
            if (format.ToUpperInvariant() == "B")
            {
                ps = from p in Lpeoples where p.Address == "北京" select p;
            }
            else if(format.ToUpperInvariant () == " S " ) 
            { 
                PS = from P in Lpeoples WHERE p.Address == " Shanghai "  SELECT P; 
            } 
            return GetContent (PS); 
        } 
        // Connect to the data array 
        Private  String GetContent ( the IEnumerable <People> Peoples) 
        { 
            Sbuilder = new new the StringBuilder ();
             the foreach ( var P in  peoples)
            {
                Sbuilder.AppendLine(string.Format("{0}{1}", p.Name, p.Address));
            }
            return Sbuilder.ToString();
        }
    }
    public class Start
    {
        public static void Main()
        {
            Peoples peoples = new Peoples()
            {new People("zhangsan","北京"),new People("lisi", " Shanghai " ), new new People ( " wangwu " , " Beijing " ), new new People ( " naliu " , " Beijing " )}; 
            Console.WriteLine ( " Site members have: " ); 
            Console.WriteLine (Peoples. ToString ()); 
            Console.WriteLine ( " Member Beijing are: " ); 
            Console.WriteLine (peoples.Tostring ( " B " )); 
            Console.The WriteLine ( " Member Shanghai are: " );
            Console.WriteLine(peoples.Tostring("S"));
            Console.ReadLine();

        }
    }
}

Additional knowledge:

  Reference IFormatProvider interface, the interface for formatting operation, when the number, date and time, the string matching operation will CultureInfo, the paper CultureInfo.CreateSpecificCulture ( "zh-CN") is intended to output the result <Simplified , China>

 

Guess you like

Origin www.cnblogs.com/jestin/p/11534475.html