June 23, 2011

Object Initializers – C# 3.0

lQuick way to create an instance and set a bunch of properties

See the below example


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


namespace ConsoleApplication7
{
    class Person
    {
        private string _firstName;
        public string firstName{get { return _firstName; }set { _firstName = value;} }
            
        
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person obj = new Person { firstName = "ABC" };
            Console.WriteLine(obj.firstName);
        }
    }
}

No comments:

Post a Comment