Generics - List collection
// Generics example.
// Build a collection which accepts only a specific class type.
// ex: collection below accepts only cPerson
namespace LearnOOp
{
public class cGenericPersoncol
{
public string BuildGenericPersons()
{
cPerson cPer = new cPerson();
cPer.Name = "J Push";
//Create a List collection of type cPerson
List cPerColl = new List();
cPerColl.Add(cPer);
//// uncomment this line to test the collection. this should throw an error if you try passing //// a cMachine type class to the List collection created earlier.
//cMachine cMac = new cMachine();
//cMac.MachineName = "Matsumito";
//cPerColl.Add(cMac);
return cPerColl[0].Name;
}
}
// Person class
public class cPerson
{
string myName;
public string Name {
get { return myName; }
set { myName = value; }
}
// Machine class
public class cMachine
{
string myMachineName;
public string MachineName {
get{return myMachineName;}
set{myMachineName = value;}
}
}
}
}
Comments