Skip to main content

C#: Program #46 (Generic Collections)

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


namespace Generics1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Animal> objects=new List<Animal>();
            objects.Add(new Animal("Hachico"));
            objects.Add(new Animal("Krypto"));
            objects.Add(new Animal("Balto"));
            objects.Add(new Animal("Gray"));
            objects.Insert(0,new Animal("Alex"));
            Console.WriteLine("The number of items in List is:{0}",objects.Count());
            foreach(Animal o in objects)
            {
                Console.WriteLine(o.Name);
            }
            objects.RemoveAt(4);
            Console.WriteLine("After Removal");
            foreach (Animal o in objects)
            {
                Console.WriteLine(o.Name);
            }
            Console.ReadKey();

        }
    }
 class Animal
    {
        public String Name{ get; set; }
        public Animal(String name)
        {
            Name=name;
        }

    }
}

Comments

Popular posts from this blog

C#: Program #52(Using Linq Extension Methods)

using System; using System.Collections.Generic; using System.Linq; namespace Linqmethods2 {     class Program     {         static void Main(string[] args)         {             List<int> series = new List<int>();             series.AddRange(Enumerable.Range(1, 20));             var squares = series.Select(a=>a*a).ToList();             foreach (int i in squares)             {                 Console.WriteLine(i);             }             Console.WriteLine("The sum of the two");             List<int> lone = new List<int>{1,5,6,3,2};             List<int> ltwo = new List...