- Dynamic lookup
- Optional and named parameters (this is a cool feature)
- Improved COM Interoperability
Quote
Let's see how this will work, let's say I have the following Employee class:
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Qualification { get; set; }
public string MiddleName { get; set; }
public Employee(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
Qualification = "N/A";
MiddleName = string.Empty;
}
public Employee(string firstName, string lastName, string qualification)
{
FirstName = firstName;
LastName = lastName;
Qualification = qualification;
MiddleName = string.Empty;
}
public Employee(string firstName, string lastName, string qualification, string middleName)
{
FirstName= firstName;
LastName= lastName;
Qualification= qualification;
MiddleName= middleName
}
}
As you can see I have 3 constructors to account for the various ways an employee can be instantiated. If I created this in C# 4.0 I could have a single parameters and utilize the new optional parameters avaiable in 4.0. The new class would look like this
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Qualification { get; set; }
public string MiddleName { get; set; }
public Employee(string firstName, string lastName, string qualification = "N/A", string middleName = "")
{
FirstName = firstName;
LastName = lastName;
Qualification = qualification;
MiddleName = middleName;
}
}
So as you can see this is a feature that will come in quite handy as we move forward with C# 4.0
Dynamic Lookup
Quote
The Dynamic Type
Quote
With the dynamic type you can finally call properties of your anonymous types outside their scope (I can see this getting seriously overused myself)> Consider this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
foreach (dynamic d in GetValues())
Console.WriteLine("{0} is {1} years old", d.Name, d.Age);
}
static IEnumerable<dynamic> GetValues()
{
Dictionary<string, int> result = new Dictionary<string, int>();
result["David"] = 30;
result["Jennifer"] = 25;
foreach (var item in result)
yield return new { Name = item.Key, Age = item.Value };
}
}
}
This code yields an IEnumerable of an anonymous type, which are used outside the scope of the Main method. This is possible only with the use of the dynamic type.
Here's another example of the power of the dynamic type. Let's say I have the following code for setting the value of a property of an object, before I would have to use
Assembly asmLib= Assembly.LoadFile(@"C:\temp\DemoClass\bin\Debug\DemoClass.dll");
Type demoClassType = asmLib.GetType("DemoClass.DemoClassLib");
object demoClassobj= Activator.CreateInstance(demoClassType);
PropertyInfo pInfo= demoClassType.GetProperty("Name");
pInfo.SetValue(demoClassobj, "Adil", null);
This can be written like this when utilizing a dynamic type
Assembly asmLib= Assembly.LoadFile(@"C:\temp\DemoClass\bin\Debug\DemoClass.dll");
Type demoClassType = asmLib.GetType("DemoClass.DemoClassLib");
dynamic dynamicDemoClassObj = Activator.CreateInstance(demoClassType);
dynamicDemoClassObj.Name = "Adil";
See how the last 3 lines can be re-written with just 2 lines, allowing me to access the properties of any type outside it's original scope.
This is not meant to be an exhaustive look at the upcoming new features, but it's a starting point to discuss your favorite upcoming features in C# 4.0

New Topic/Question
Reply


MultiQuote









|