June 23, 2011

Code example for Collection Initializers – C# 3.0

Quick way to add items in collection
below is the example


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


namespace ConsoleApplication7
{
  public   class Person
    {
        private string _firstName;
        public string firstName{get { return _firstName; }set { _firstName = value;} }
            
        
    }




    class Program
    {
        static void Main(string[] args)
        {
            List<Person> obj=new List<Person> 
            {
                new Person {firstName ="ABC"},
                new Person {firstName ="Test"}


            };


            foreach (Person p in obj)
            {
                Console.WriteLine(p.firstName);




            }
        }
    }
}

1 comment: