C#.NET 4.0 new features


C# as a language is getting richer day by day whilest they are introduced new release of VS2010. We are having many new features on the .NET framework, ASP.NET and C#.NET on the Visual Studio 2010 version. The new features on C#.NET 4.0 are Dynamic Keyword and Named and Optional Parameters. Find C#.NET 4.0 new features here.

About C#.NET 4.0 new features


Dynamic Keyword:


The Dynamic Language Runtime (DLR) is a new API in .NET Framework4.0, it provides the infrastructure that supports the dynamic type in C# and also the implementations of IronPython and IronRuby dynamic programing languages. The DLR is sits above the Common Language Runtime (CLR) and bypasses the compile time check.

The Dynamic type introduce in C# 4.0. It is a static type, but these dynamic type objects bypass compile-time type checking that's why types of these placeholders are unknown until runtime. This is totally not a new technology.
Below is the sample program how to use the Dynamic keyword.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace myDynamic
{
class Program
{
static void Main(string[] args)
{
DynamicExample ec = new DynamicExample();


dynamic dynamic_ec = new DynamicExample();

dynamic_ec.DynamicExampleMethod2(10, 4);


dynamic_ec.someMethod("some argument", 7, null);
dynamic_ec.nonexistentMethod();

}
class DynamicExample
{
public DynamicExample()
{
}
public DynamicExample(int v)
{
}

public void DynamicExampleMethod1(int i)
{
}

public void DynamicExampleMethod2(string str)
{
}
}

}
}


From the above code I have used the dynamic type is assigned to DynamicExample class and called the DynamicExampleMethod2. Here DynamicExampleMethod2 method having only one input parameter, but I passed two parameters. The error will come at dynamic time.

Named and Optional Parameters:


Named Parameters:


C# 4.0 introduced a great feature in object oriented programing approach that is named parameters. Previously we have arranged and assign the parameters carefully and serially. But now we have a concept called Named Parameter, by using the Parameter name we have to assign he value, you can forget the order. Below is the sample example to demonstrate the named parameters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NamedVaruables
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Add(123, 64));

Console.WriteLine(Add(A: 123, B: 64));

Console.WriteLine(Add(B: 64, A: 123));

Console.WriteLine(Add(123, B: 64));
}

static int Add(int A, int B)
{
return (A+B);
}

}
}


Optional Parameters:


We already have this feature in VB.Net, but recently introduced in C# 4.0. Below is the VB.NET code.

Public Class OptionalParameterDemo

Public Function MySampleOptionalParameter(ByVal x As String, Optional ByVal y As Integer = 10) As String
Return String.Format(x, y)
End Function

Sub MyCall()
Me.MySampleOptionalParameter("Will take optional parameter as {0}")
End Sub
End Class


We can define the optional parameters like as below. We can declare default parameter values at the method declaration and pass the variables as required.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OptionalParameters
{
class Program
{
static void Main(string[] args)
{
clsOptionalParameters MyExample = new clsOptionalParameters();
MyExample.ExampleMethod(1, "One", 1);
MyExample.ExampleMethod(2, "Two");
MyExample.ExampleMethod(3);


clsOptionalParameters MyAnotherExample = new clsOptionalParameters("Provided name");
MyAnotherExample.ExampleMethod(1, "One", 1);
MyAnotherExample.ExampleMethod(2, "Two");
MyAnotherExample.ExampleMethod(3);

MyExample.ExampleMethod(3, optionalint: 4);
}
}

class clsOptionalParameters
{
private string _name;

public clsOptionalParameters(string name = "Default name")
{
_name = name;
}


public void ExampleMethod(int required, string optionalstr = "My default string", int optionalint = 10)
{
Console.WriteLine("{0}: {1}, {2}, and {3}.", _name, required, optionalstr,
optionalint);
}
}


}



Comments

Guest Author: Pendi20 Feb 2012

I was only thinking about implementation of decisions like size and the like but clearly it's got usages way beyond that that nobody else needs to describe since it should have been obvious.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: