Cyberiafreak

"Fortunate are those who take the first steps.” ― Paulo Coelho

C# 4.0 new keyword and FAQ

1. Dynamic lookup

 

1.1) What is Dynamic lookup?

It’s a new static type, allows to write method, operator and indexer calls, property and field accesses etc gets resolved at runtime. Object is assumed at compile time to support any operation, and only at runtime will you get an error if it wasn’t so.

 

1.2) Any example for Dynamic lookup?

dynamic d = GetDynamicObject(10);

d.M(7);

               dynamic value = 10;

               value.somedynamicoperation(); //resolved at runtime else exception 

 

1.3) Difference between var and dynamic keyword?

                varthe compiler knows the value and type at runtime

dynamic- resolve value assigned to it at run time. it is similar to object, but with dynamic dispatch: It can resolve properties, methods ,operators etc at runtime 

static void Main(string[] args)

{

string s = "this is a test string";

Console.WriteLine(s.Length);

var v = "this is a test string";

Console.WriteLine(v.Length); 

dynamic d = "this is to test dynamic";

// it wont throw compilation error but will bomb during runtime

// Exception as "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException" – ‘string’ does not contain a definition for ‘Something’

Console.WriteLine(d.Something());   // runtime error for missing method

Console.WriteLine(d.Length); // runtime error for missing prop

}

 

1.4) When to use dynamic & benefit ?

Useful when interacting with COM (which can have runtime-only properties), the DLR, or other dynamic systems, like javascript. Added to the CLR in order to support dynamic languages like Ruby and Python.

 

Read more about here : http://blogs.msdn.com/csharpfaq/archive/2010/01/25/what-is-the-difference-between-dynamic-and-object-keywords.aspx

 

2.Named & Optional Params

Both are different feature, will see first for Named parameter.

 

2.1 What is Named Parameter in C# 4.0?

Parameters are allowed to passed by naming rather following the order. We can pass the exact parameter leaving the default with explicit naming.

SomeMethod(s1: "UserProvided1", s3:"UserProvided3");// caller

private static void SomeMethod(string s1 = "Default1", string s2 = "Default2", string s3 = "Default3")

        {

       Console.WriteLine(s1);

Console.WriteLine(s2);  //omitted by the caller but displays default value               

Console.WriteLine(s3);

        }

 

Output:

UserProvided1

Default2

UserProvided3

 

2.2 What are optional parameters?

It allows us to call a method with provided optional parameter..

       CalculateSum(10); //param 2,3,4 are optional, but param1 is mandate since there is no default value defined

       CalculateSum(10, 20); //param 3,4 are optional

       CalculateSum(10, 20, 30); //param 4 are optional           

      private static void CalculateSum(int a, int b=0, int c = 0, int d = 0)

        {

            Console.WriteLine(a + b + c + d);

        }

 

3. Features for COM interop

In order to have better interop performance at the COM level, few new features are added..such as Dynamic import, Compiling without PIAs,Omitting ref,Open issues etc to support better COM connectivity

 

4. Variance

It used to be that an IEnumerable<string> wasn’t an IEnumerable<object>. Now it is – C# embraces type safe “co-and contravariance” and common BCL types are updated to take advantage of that.

In C#, variance is supported in the following scenarios:

1.               Covariance in arrays (since C# 1.0)

2.               Covariance and contravariance in delegates, also known as “method group variance” (since C# 2.0)

3.               Variance for generic type parameters in interfaces and delegates (since C# 4.0)

 

Leave comments if you find good example for the Variance.

-Mahes

Advertisements

March 12, 2010 - Posted by | Uncategorized

No comments yet.

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

%d bloggers like this: