New features of c# 4.0
This Article basically covers on the New Features that are added to c# 4.0. We will cover this in details with examples for each and every future that we are going to look in. The new features will help us in doing our code quickly with ease.
The new features that are added to C# 4.0 are
1. Named Parameters.
2. Optional Parameters.
3. Dynamic
4. Expando Object
5. Tuple
Will now go through each one in detail
Named Parameters
1. The values are assigned to the parameters by their name
2. The Assigning of values to the parameters can be of any order
static string EmpDetails(string firstName,string lastName,int Age,string Dept)
{
return firstName + " " + lastName + " " + Age + " " + Dept;
}
// named parameter
// Here we are assigning values of the parameter by the name of the Param
richTextBox1.AppendText("\n"+ EmpDetails(lastName: "Vathy", firstName: "Padma", Dept: "Medicine", Age: 27));
Optional Parameters
1.Default Values to the parameters are given while creating the method
2.Either we can live with the default value or can pass value to the method while invoking the method
3.But make sure that optional parameters should be last one
static void Print(string document_name, int nbr_of_pages = 1, string color = "BW")
{
string status = String.Format
("Going to print {0} document {1} page(s) in {2}", document_name, nbr_of_pages, color);
MessageBox.Show( status) ;
}
//Invoking Methods - Optional Parameters
PrintDocument("c:\\resume.txt");
PrintDocument("c:\\resume.txt", 10, "Color");
3. Dynamic
1. Its operations are checked at Runtime. Due to this you will not get error while compiling the program
2. We should try avoiding using this Dynamic Type. It should be used only when situation demands. Otherwise it's a costly process.
3. it's mainly used while calling using Reflection or COM
class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Department { get; set; }
public string GetFullName()
{
return string.Concat(FirstName, "-", LastName,"-",Department);
}
}
dynamic x = "Test Dynamic";
MessageBox.Show(x.GetType().Name);
x = 100;
MessageBox.Show(x.GetType().Name);
dynamic dyn = new Employee();
dyn = new Employee() { FirstName = "Varshika", LastName = "B", Department = "Medicine" };
richTextBox1.Text = "\nafter casting : " + (dyn.GetType());
richTextBox1.AppendText("\n new values : " + dyn.FirstName + " - " +dyn.LastName + " -" + dyn.Department);
4. Expando Object
1. It helps us in adding and removing members at runtime
StringBuilder sb = new StringBuilder();
dynamic expObj = new System.Dynamic.ExpandoObject();
expObj.FirstName = "Varshika"; // Dynamically adding Members
expObj.LastName = "B"; // Dynamically adding Members
sb.Append(expObj.FirstName + " " + expObj.LastName);
//adding a new function
Func
today => today.AddDays(1).ToShortDateString();
expObj.GetNext = GetNextDay;
sb.Append("\n Tomorrow's Date is " + expObj.GetNext(DateTime.Now));
richTextBox1.Text = sb.ToString();
5. Tuple
1. Tuple is a static class
2. It is mainly used for creating object groups.
3. The return type is also Tuple
4. It can hold maximum of 7 parameters.
5. The last member item can be one more Tuple.
//Sample -1
//Syntax
var empDetails = Tuple.Create
richTextBox1.AppendText("Sample-1 \n \n Syntax\n");
richTextBox1.AppendText("\n\n Tuple.Create
richTextBox1.AppendText ("\n Output \n" + empDetails.ToString());
MessageBox.Show(empDetails.Item1.ToString() + " - " + empDetails.Item2.ToString() + " - " + empDetails.Item3.ToString(), "TUPLE DEMO");
//Sample-2
//Syntax
// Assigning one more Tuple to Tuple at end
var empDetail = Tuple.Create
("Prem", "kumar", Tuple.Create
MessageBox.Show(empDetail.Item1.ToString() + "- " + empDetail.Item2.ToString() + "-" + empDetail.Item3.Item1 + "-" + empDetail.Item3.Item2);// raj - kiran
richTextBox1.AppendText("Output \n" + empDetails.ToString());