C # generics.

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Collections;
Using System. Collections. Specialized;

Namespace ConsoleApplication1
{
Class Program
{
Static void Main (string [] args)
{
ArrayList myList = new ArrayList (20 );
MyList. Add (20); // boxed operation.

MyList. Add (1, 202 );

Int i1 = (int) myList [0]; // binning

Foreach (int item in myList)
{
Console. WriteLine (item); // This program is about ArrayList.
}

List <int> myListT = new List <int> (10 );
MyListT. Add (412); // No packing operation

Int i2 = myListT [0]; // it will not be split.

Foreach (int item in myListT)
{
Console. WriteLine (item );
}
// It is worth noting that if there are different types when values are included, but an error is reported when the foreach loop is used,
// You can avoid this situation by using generics. An error is reported during compilation.

}
Public class MyOK // below are some examples of generics
{
Public delegate void EventHandler <TEventArgs> (object sender, TEventArgs e );

Public delegate TOutput Converter <TOutput, TIntput> (TIntput from );


}
Public class SortedList <Tkey, Tvalue>
{
 
}

}

}

 

//////////////////////////////////////// //////////////////////////////////////// //////

// The following is another example. About the FindAll method and Find // of List <T> //

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Collections;
Using System. Collections. Specialized;

Namespace generic
{
Public class Racer
{
Private string name;

Public string Name
{
Get {return name ;}

}
Private string car;

Public string Car
{
Get {return car ;}

}

Public Racer (string name, string car)
{
This. name = name;
This. car = car;

}
Public override string ToString ()
{
Return string. Format ("Name: {0,-20} Car: {1}", Name, Car );
}

}

Public class MainRun
{
Static void Main ()
{
List <Racer> racers = new List <Racer> ();
Racers. Add (new Racer ("James", "Audi "));
Racers. Add (new Racer ("Li Si", "rotten car "));

// Foreach (Racer item in racers)
//{
// Console. WriteLine (item );
//}
FindRacer finder = new FindRacer ("Audi ");

Foreach (Racer item in racers. FindAll (new Predicate <Racer> (finder. DrivingCarPredicate) // This is the FindAll of the generic List.
{
Console. WriteLine (item );
}

}

}

Public class FindRacer
{
Private string car;
Public FindRacer (string car)
{
This. car = car;

}

Public bool DrivingCarPredicate (Racer racer)
{
Return racer. Car = car;
}
}

}

 

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.