c#4.0 features

13
.NET 4.0 FEATURES

Upload: yaswanth-babu

Post on 19-Jul-2015

120 views

Category:

Education


4 download

TRANSCRIPT

.NET 4.0 FEATURES

NAMED AND OPTIONAL ARGUMENTS

Optional Arguments:

Optional arguments enable you to omit arguments for some parameters. When a function has an optional parameter, the caller can decide whether to

provide a value or not. If the caller doesn't provide a value, the function uses a default value.

Named Arguments:

Named arguments enable you to specify an argument for a particular parameter by associating the argument with the parameter's name rather than with the parameter's position in the parameter list.

Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods.

Calculate(weight: 123, height: 64);

Dynamic: Dynamic is a new static type that acts like a placeholder for a type

not known until runtime. At compile time, an element that is typed as dynamic is assumed to

support any operation.Ex:dynamic dynamic_ec = new ExampleClass(); // The following line is not identified as an error by the// compiler, but it causes a run-time exception. dynamic_ec.exampleMethod1(10, 4); //no method with two parameters

VAR DYNAMIC

Introduced in C# 3.0 Introduced in C# 4.0

Statically typed – This means the type of variable declared is decided by the compiler at compile time.

Dynamically typed - This means the type of variable declared is decided by the compiler at runtime time.

Need to initialize at the time

of declaration.

No need to initialize at the

time of declaration.

Errors are caught at compile time.

Errors are caught at runtime

Visual Studio shows intellisense since the type of variable assigned is known to compiler.

Intellisense is not available since the type and its related methods and properties can be known at run time only

Reflection Dynamic

Inspect (meta-data) Yes No

Invoke public members Yes Yes

Invoke private members Yes No

Caching No Yes

Static class Yes No

Covariance and Contravariance:Covariance: Covariance preserves assignment compatibility. An object of a more derived type is assigned to an object of a less

derived type.Contravariance: An object that is instantiated with a less derived type argumentis assigned to an object instantiated with a more derived type argument. Assignment compatibility is reversed.

// Assignment compatibility.string str = "test";// An object of a more derived type is assigned to an object of a less derived type.object obj = str;// Covariance.IEnumerable<string> strings = new List<string>();IEnumerable<object> objects = strings;// Contravariance.// Assume that the following method is in the class:// static void SetObject(object o) { }Action<object> actObject = SetObject;Action<string> actString = actObject;

Dynamic Language Runtime: The dynamic language runtime (DLR) is a runtime environment that

adds a set of services for dynamic languages to the common language runtime (CLR).

The DLR makes it easier to develop dynamic languages to run on the .NET Framework and to add dynamic features to statically typed languages.

Dynamic languages can identify the type of an object at run time, whereas in statically typed languages such as C# and Visual Basic you must specify object types at design time.

Examples of dynamic languages are Lisp, Smalltalk, JavaScript, PHP, Ruby, Python, ColdFusion, Lua, Cobra, and Groovy.

Expression Trees: The DLR uses expression trees to represent language semantics. For this

purpose, the DLR has extended LINQ expression trees to include control flow, assignment, and other language-modeling nodes.

Call Site Caching: A dynamic call site is a place in the code where you perform an operation on

dynamic objects. The DLR caches the characteristics of a and b (usually the types of these

objects) and information about the operation.Dynamic Object Interoperability: The DLR provides a set of classes and interfaces that represent dynamic

objects and operations and can be used by language implementers and authors of dynamic libraries.

int Fibonacci(int n){if (n < 1){return 1;}else{return Fibonacci(n ‐ 1) + Fibonacci(n ‐ 2);}}