Let us play around with some new features of C# 3.0 or above and you can say .Net framework 3.0 and above, like Object initializer, implicitly typed variables, extension methods, anonymous types, object initializer, Collection initializer, and automatic properties. Let's start with Object and collection initializes.Just refer to the following code of style. Definitely all .Net coders are aware of these lines....
...Regular method of initializing an instance of the Customer class. Phone oPhone = new Phone();
oPhone.CountryCode = "+91";
oPhone.AreaCode = "0999";
oPhone.PhoneNumber = "999999"; Customer oCustomer = new Customer();
oCustomer.CustomerID = 101;
oCustomer.CompanyName = "oTest Corporation";
oCustomer.Phone = oPhone; By taking advantage of Object Initializers an instance of the Customer classCustomer nCustomer = new Customer()
{
CustomerID = 102,
CompanyName = "nTest Corporation",
Phone = new Phone()
{
CountryCode = "+91",
AreaCode = "09999",
PhoneNumber = "999999"
}
};
Read more: C# Corner
QR:
...Regular method of initializing an instance of the Customer class. Phone oPhone = new Phone();
oPhone.CountryCode = "+91";
oPhone.AreaCode = "0999";
oPhone.PhoneNumber = "999999"; Customer oCustomer = new Customer();
oCustomer.CustomerID = 101;
oCustomer.CompanyName = "oTest Corporation";
oCustomer.Phone = oPhone; By taking advantage of Object Initializers an instance of the Customer classCustomer nCustomer = new Customer()
{
CustomerID = 102,
CompanyName = "nTest Corporation",
Phone = new Phone()
{
CountryCode = "+91",
AreaCode = "09999",
PhoneNumber = "999999"
}
};
Read more: C# Corner
QR: