Object and Collection Initializers Feature in C# 3.0
C# 3.0 introduced the another interesting feature Object and Collection initialization expressions.
Object Intialization Expressions allows you to initialize an object without invoking the constructor and setting its properties.
If you take Employee Class as an Example:
public class Employee {
private int iEmpId;
private string strFirstName;
private string strLastName;
public int ID{
get{
return iEmpId;
}
}
public string FirstName {
get {
return strFirstName;
}
set {
strFirstName= value;
}
}
public string LastName {
get {
return strLastName;
}
set {
strLastName = value;
}
}
public Employee() {}
}
We can use Object Intialization Expressions in C#3.0 Features to create a Employee as follows:
Employee objEmployee = new Employee {ID=007, FirstName = “Kalyan” LastName = “Bandarupalli” }
Now observe the above code we have neither invoked the constructor nor set the any properties directly. The code above is equivalent to the following code:
Employee objEmployee = new Employee();
objEmployee.ID = 007;
objEmployee.FirstName = “Kalyan”;
objEmployee.LastName = “Bandarupalli”;
We can use Collection Intialization Expressions in C#3.0 Features to create a list of Employees as follows:
List<Employee> listofEmployees = new List<Employee>
{ {objEmployee.ID = 007, objEmployee.FirstName = "Kalyan”},
{objEmployee.ID = 007, objEmployee.FirstName = "Suresh”},
{objEmployee.ID = 007, objEmployee.FirstName = "Naveen”}
};
The advantage of using this feature is that it saves your time for creating a lot of constructors or initializing the individual property.
Related Posts:
2 commentsAutomatically Implemented Properties Feature in C# 3.0
Introduction of Automatic Properties in C# 3.0 make property declaration more concise. It saves some of your time in typing a lot of code.
Example:
class Employee
{
public string FirstName { get; set; }
}
Benefit of using Automatic Properties
We have been creating properties in so many projects and notice that we are writing so many lines of code just for creating a simple property.
Example: Employee class that does not use Automatic Properties
public class Employee {
private int iEmpId;
private string strFirstName;
private string strLastName;
public int ID{
get{
return iEmpId;
}
}
public string FirstName {
get {
return strFirstName;
}
set {
strFirstName= value;
}
}
public string LastName {
get {
return strLastName;
}
set {
strLastName = value;
}
}
}
In order to create a simple property, we are coding extra four or five lines code. So, if we have 100 properties in different entity classes, we have to type too much of code just for creating properties.
If you convert above Employee class with Automatic properties it look like
class Employee
{
public int ID{ get; private set; } // read-only
public string FirstName { get; set; }
public int LastName { get; set; }
}
Note: This is my first article to explain the C# 3.0 tutorial series.
Conclusion
It is one of C# 3.0 Language feature and nothing new except it helps you to improve your productivity.
Related Posts:
2 commentsTest driven development using C#
This post explains how TDD can be implemented in C# using NUNIT.
You can read the Introduction TDD to get an idea on TDD.
Nunit is a open source framework to for .NET which helps you to automate the unit testing. Nunit can downloaded from the Nunit site. current version is 2.5.
Nunit provides two utilities for running the automated tests
nunit-gui.exe – GUI toolnunit-console.exe – Command line tool
Using Nunit in Visual studio .NET Project
To implement the TDD we need to write
- Test cases
- program that is having test cases to run against it
Behavior of the Person C# class that we are going to write test cases
The Behavior can be described as follows
- A Person has properties age,full name and cash balance.
- When instantiating a person we should able to provide a first name, last name and age.
- The person full name property should return first name and last name with a space in-between.
- When you are creating person object cash balance should be 20000.
- The cash balance should be reduced by the amount he with draws.
From the Above behavior we can formulate a number of test cases example
- If we create a person with first and last name of “Kalyan” and “Bandarupalli” , and age 27, the person’s full name should not be ” Kalyan Hero”.
- The person full name should be “Kalyan Bandarupalli”.
- The person’s age should be 27.
- Before withdrawing money the person’s cash balance should be 20000.
- After withdrawing 10000 cash, the person’s cash balance should be 10000.
We can implement the above test case in code[C#] using Nunit. To create a test case in your application create a class having the [TextFixture] attribute.
This class will contain test to test the methods,operations and values in Person class.
Related Posts:
2 commentsFactory method Designpattern using C#
The factory method pattern is a creational design pattern used in software development to encapsulate the process of creating the objects.
Concerns:
- Which object needs to be created.
- Managing the life time of the object.
- Managing the build-up and tear down concerns of the object.
Definition:
“Define an interface for creating an object, but let subclasses decide which class to instantiate”
C# Implementation of Factory method
abstract class Factory
{
public abstract Product GetProduct(); //Factory Method Declaration
}
——————————————————————————————-
class concreteFactoryforProcuct1 : Factory
{
public override Product GetProduct() //Factory Method Implementation
{
return new Product1();
}
}
——————————————————————————————–
class concreteFactoryforProcuct2 : Factory
{
public override Product GetProduct() //Factory Method Implementation
{
return new Product2();
}
}
——————————————————————————————–
interface Product
{
void GetDetails();
}
class Product1 : Product
{
public void GetDetails()
{
Console.WriteLine("Product1 Details are Called");
}
}
class Product2 : Product
{
public void GetDetails()
{
Console.WriteLine("Product2 Details are called");
}
}
——————————————————————————————–
protected void Page_Load(object sender, EventArgs e)
{
Factory[] objFactories = new Factory[2];
objFactories[0] = new concreteFactoryforProcuct1();
objFactories[1] = new concreteFactoryforProcuct2();
foreach (Factory objFactory in objFactories)
{
Product objProduct = objFactory.GetProduct();
objProduct.GetDetails();
}
}
——————————————————————————————–
Related Posts:
5 commentsC# 3.0 features
The following features are introduced in the C# 3.0
Visual studio C# 2008
1. Lambda Expressions.
2. Extension Methods.
3. LINQ Query Expressions.
4. Anonymous Types.
5. Expression Trees.
6. Partial Methods.
7. Auto-Implemented Properties.
8. Object and Collection Intializers.
Related Posts:
1 commentC# 2.0 features
The following features are introduced in the C# 2.0
[Visual studio C# 2005 ]
1. Generics.
2. Anonymous Methods.
3. Iterators.
4. Partial Types.
5. Nullable Types.
6. Delegate Inference.
7. Covariance and Contravariance.
8. Friend Assemblies.
9. # Pragma warning.
10. Captured Variables.