Add user-defined object to generic Collection : Generic Collections : Generics C# Source Code


Custom Search

C# Source Code » Generics » Generic Collections »

 

Add user-defined object to generic Collection








    
 
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;

class Team {
    private string name;

    public Team(string name) {
        this.name = name;
    }

    public override string ToString() {
        return name;
    }
}

class Program {
    static void Main(string[] args) {
        Collection<Team> teams = new Collection<Team>();
        teams.Add(new Team("Ferrari"));
        teams.Add(new Team("Williams-BMW"));
        teams.Add(new Team("Bar-Honda"));
        teams.Add(new Team("McLaren-Mercedes"));

        foreach (Team t in teams) {
            Console.WriteLine(t);
        }
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Generics
» Generic Collections