Lets move to C# 4.0

Posted on Updated on

Some features like Dynamic programming,Optional arguments,Named arguments

Dynamic Programming :

This is the major feature of C# 4.0. In the first look it looks something like our previous “var” type of C#3.0 or regular object type that we have with C#,root Type of .NET . But its not like that. Actually when you assigns a variable of Type “dynamic” and use its properties/methods ,compiler doesn’t mind it at compile time. Means, no method/property resolution takes part in compile time and it will take care all of those things at run time. By these words dont think “dynamic” is dynamic type ,its a static type only.

The below mentioned code is a small example for the dynamic programming.

using System;
using System.Text;

namespace DynamicType
{
	class Program
	{
		static void Main(string[] args)
		{

			Mathematics objmathematics = new Mathematics();
			dynamic objdynamic = objmathematics;

			
			double resAdd= objdynamic.add(3,2);
			Console.WriteLine(resAdd.ToString());

			//The method "subtract" is not present in the Class Mathematics.
			double resSub = objdynamic.subtract(3, 2);
			Console.WriteLine(resSub.ToString());

			Console.ReadLine();
		}
	}

	class Mathematics
	{ 
	
		public Mathematics()	{}


		public double add(double a, double b)
		{
			return a + b;		
		}
	}
}

On compiling the above code, the build will succeed with a smile. But at run time it will break at code line number 18. Because the function “subtract” that is trying to resolve is not present in the Mathematics Class and method “add” will pass through because it is available.

The DLR, Dynamic Language Runtime is responsible for this dynamic programming capability . Basically its built on the top of the CLR to make the dynamic languages such as IronPython, IronRuby etc to get supported. To know more about DLR please go to this link

The main use of this dynamic programming is to support the compilation during interfacing of our managed code with COM , Javascript DOM or any dynamic languages.


Optional and Named arguments:

Now we can have optional values for the C# methods . We can set default values for the arguments and can pass the values during the method call according to our need.Also we can call each parameter by its name , not by its position as earlier. This is called Named argument

It will be like this :

public double GetPNRValue(double a=1,double b=0,double c=1)
		{
			return (a * b / c);
		}

And when we call this method, we can pass arguments like this

double resPNR = objdynamic.GetPNRValue(2,c:0);			

It says that “a” is 2, “b” can be the optional argument and “c” be zero . Here “c” is the Named argument.

Advertisements

2 thoughts on “Lets move to C# 4.0

    Manoj said:
    July 16, 2010 at 1:29 pm

    I am confused by the statement “By these words dont think “dynamic” is dynamic type ,its a static type only.” in your blog. Static typing make sure that the compiler catches the errors early. Based on the type of the variable, it will see if the variable/method are accessible etc. But in the case of dynamic typing all these happen at runtime. So if the method is not defined on a type, then there is no way to call it. Simple as that.

    dynamic keyword enables ‘duck typing’- an example below. If the same program were to be implemented without ‘dynamic’ keyword, then I would have to create an Interface and the 2 classes would then have to implement those interfaces.

    using System;
    using System.Text;

    namespace DynamicType
    {
    class Program
    {
    public static void Main(string[] args) {

    Mathematics objmathematics = new Mathematics();
    callDynMethod(objmathematics, 1, 2);

    Complex objComplex = new Complex();
    callDynMethod(objComplex, 4,5);

    Console.ReadLine();
    }

    public static double callDynMethod(dynamic objdynamic, double a, double b)
    {
    double resAdd= objdynamic.add(a,b);
    Console.WriteLine(resAdd.ToString());
    return resAdd;
    }

    }

    class Mathematics
    {

    public double add(double a, double b)
    {
    return a + b;
    }
    }

    class Complex
    {
    public double add(double real, double imaginary)
    {
    return real + imaginary;
    }

    }
    }

    Abin Jaik Antony responded:
    July 17, 2010 at 11:56 am

    Hello manoj,
    Basically the “dynamic” type behaves like the System.Object. At compile time it bypasses the static type checking and it supports any operation. At runtime it resolves the stored information at compile time and invalid statements will throw a runtime exception. Actually this is done with help DLR (Dynamic language runtime), new runtime feature which got added to CLR. This DLR enables the dynamic features in Statically typed Languages like C#,VB.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s