c sharp 4.0 features

1.       What is major difference between different version of c# languages?

Ans:  C# 1.0 for managed code, C#2.0 included Generics or Template, c# 3.0 included Language Integrated Query(LINQ) and Now C# 4.0 Includes dynamic programming. There are breaking changes in different version of C# language or compiler.

2.       What are new features of C# 4.0?

Ans:  Dynamically Typed Objects, Optional and Named Parameters, Improved COM Interoperability, Co- and Contra-variance.

3.       How dynamic programming works?

Ans: With help of DLR(Dynamic Language Run Time).

4.       What is dynamic type?

Ans:  dynamic is a new type only in the compiler. Encoded in IL as object + DynamicAttribute. There is no dynamic type in the CLR. There is an implicit conversion from dynamic to any type and supports all types of operation on CLR like Method call, property access, indexer access, operators, conversions.

5.       What is optional and Named parameter in c#?

Ans: Named argument does not require that you remember the sequence of parameters.

  For Example:

    public class Sample

    {

        public static void main()

        {

            Sample obj = new Sample();

            obj.SetName(“sheel”, “Prabhakar”);

            obj.SetName(LastName: “Prabhakar”, FirstName: “Sheel”);

            obj.SetName(“FirstName”, LastName: “LastName”);

        }

        public void SetName(string FirstName, String LastName)

        {

 

        }

    }

Named arguments also improve the readability of your code by identifying what each argument represents.

A named argument can follow positional arguments, as shown here.

obj.SetName(“sheel”, LastName: “prabhakar”);

 But Positional argument can not follow a named argument.

 

Optional Parameter: In C# any constructor, method, delegate can specify which parameter are required and which are optional. A caller function must provide values for all required parameter and can omit optional parameters.

Each optional param has a default value(It is just like c++ default vaue param).

Optional parameter must be in last sequence of parameter list.

If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list are not supported( As it is supported in vb6.0 as per my knowledge).

    public class Sample

    {

        public static void main()

        {

            Sample obj = new Sample();

            obj.SetName(“sheel”);

            obj.SetName(FirstName: “Sheel”);

            obj.SetName(“FirstName”, LastName: “LastName”);

        }

        public void SetName(string FirstName, String LastName=“Default Last Name”)

        {

 

        }

    }

Important : The default value for a parameter is encoded with a DefaultParameterValue attribute

The compiler inserts that value if a value is not specified for that parameter.

If a default value is not specified we will use default(type).

For COM, the compiler will pass in Type.Missing.

The compiler will rearrange the specified named parameters and then apply overload resolution.

6.       What is co and contra varience in c# 4.0

Ans: http://blogs.msdn.com/b/wriju/archive/2009/07/31/c-4-0-co-variance-and-contra-variance.aspx

Leave a Reply

Your email address will not be published. Required fields are marked *