Implement custom indexing operator

Custom index operator

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

namespace implement custom index operator 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            // Console.WriteLine ( "the Hello World!"); 
            var P1 = new new the Person ( " Ayrton " , " . Senna " , new new the DateTime ( 1960 , . 3 , 21 is ));
             var P2 = new new the Person ("Ronnie","Peterson",new DateTime(1944,2,14));
            var p3 = new Person("Jochen","Rindt",new DateTime(1942,4,18));
            var p4 = new Person("Francois","Cevert",new DateTime(1944,2,25));
            var coll = new  PersonCollection(p1,p2,p3,p4);
            System.Console.WriteLine(coll[2]);
            foreach(var r in coll[new DateTime(1960,3,21)]){
                System.Console.WriteLine(r);

            }
            System.Console.ReadLine();

            //输出结果如下
            // Jochen Rindt
            // Ayrton Senna
        }
    }

    public class Person
    {
        public DateTime Birthday{get;}
        public string FirstName{get;}
        public string LastName{get;}

        public Person(string firstName,string lastName,DateTime birthday){
            FirstName = firstName;
            LastName = lastName;
            Birthday = birthday;

        }

        public override string ToString()=>$"The LastName FirstName} {} { " ; 
    } 

    public  class PersonCollection 
    { 
        Private the Person [] the _People;
         public PersonCollection ( the params the Person [] people) => _ people = people.ToArray (); 
         // To allow access using the index syntax and returns PersonCollection Person object can create an indexer. 
        public Person the this [ int index] 
        { 
            // call the value is retrieved when invoking get accessor, passing Person object in the right set accessor. 
            get => the _People [index];
             set => _ people [index] = value; 
        } 

        //This index is used to specify the return has everyone's birthday, because many people may have the same birthday, so use the interface
         // IEnumerable <Person> returns a Person object list.
        // public the IEnumerable <the Person> the this [the DateTime Birthday]
         // {
         //      GET => _ people.Where (P => == p.Birthday Birthday);
         // } 
        public the IEnumerable <the Person> the this [the DateTime Birthday] => _people.Where (P => == p.Birthday Birthday); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/singhwong/p/11930568.html