Using Generics with Custom Classes in C#

Person Class:

public class person
{
    string firstName;
    string lastName;
    public person(string _firstName, string _lastName)
    {
        firstName = _firstName;
        lastName = _lastName;
    }
    override public string ToString()
    {
        return firstName + " " + lastName;
    }
}

Using the person class in a sortedlist

SortedList<string, person> sl = new SortedList<string,person>();
sl.Add("One", new person("kak", "poi"));
sl.Add("Two", new person("tim", "yahoo"));
sl.Add("Three", new person("mani", "item"));
foreach (person p in sl.Values)
Console.WriteLine(p.ToString());

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.