Saturday, February 13, 2010

Dotnet New initialization feature

There are different ways an object properties can be initialized
  • Using constructor
  • Calling some methods
  • Or directly updating the values by object

But there is a new feature implemented in Dotnet

Consider the following class

   class Person
    {
        public string FirstName { get; set; }
        private string LastName { get; private set; }
        protected string Email { get; set; }
        public int Age { get; set; }
    }

if I have to assign values to the properties in the new sexy way you will do something like below


   class Program
    {
        static void Main(string[] args)
        {
            Person f = new Person()
            {
                FirstName="s",Age=12
            }
            ;

        }
    }

You can assign/initilaize only the public properties in this fashion
Post a Comment