-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Anand_2004
Message 1 in Discussion
Hi Folks , Today I am talking about Object Initializer Object initializer
is a new language enhancements of C# 3.0.This helps to initialize the initial
value to the specified data member(s) of the newly created object or
collection in a single step , {} braces used to initialize the data members
as below
using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
namespace Object_Initializers
{
class Employee
{
public int EmpNo;
public string EmpName;
static void Main(string[] args)
{
Employee empObj = new Employee{EmpNo = 1,EmpName="Anand"};
Console.WriteLine("The Emp No # {0} Emp Name #
{1}",empObj.EmpNo,empObj.EmpName);
Console.ReadLine();
}
}
}
This is equals to invoke a instance constructor and assign value to each
field . Each member initializer assigns a value to a field or property of the
object . the object initialize applicable to collection class as well .The
process of initialize the collection object is similar to the above code .
List<string> listObj = new List<string> {"UG Hyderabad","UG Bangalore","UG
Chennai","UG Mumbai"};
foreach ( string s in listOobj)
Console.WriteLine(s);
Few points you must keep in mind while using the object initializer 1.
The Member initializer must be a field or property (set property) of the
object
2. You can not initialize multiple values to the same field or property
3. The initializer applies to the collection of type
System.Collections.Generic.ICollection<T>
Cheers Anand https://anandkumar2004.wordpress.com
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you
received this message by mistake, please click the "Remove" link below. On the
pre-addressed e-mail message that opens, simply click "Send". Your e-mail
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]