This blog is subject the DISCLAIMER below.

Thursday, February 08, 2007

What's new in C# 3.0 ? Part 1

in the name of ALLAH

Sorry for being late to write this article.
as c# 3.0 came with many new features, i will discuss every one in a single post.

First i will discuss the difference between the static languages and dynamic languages
coz there are many of people say ( C# 3.0 became dynamic language ) and sure this is wrong

static lang. : - every variable has its own data type, and we can't give the variable any value not compatible with its type, eg .. if int x=8; we can't say x="fci-h"; this will raise an error.
examples for the static langs. : - c++ , java and c#.

dynamic langs. : - where the data type of any variable can be changed Automatically during the running time according to the value of this variable.
examples for the dynamic langs. : - python.

now lets start :D

implicitly typed local variables (using Var keyword)

In the implicitly typed local variables the data type is being inferred by the initialization value of this variable during the compilation time, eg…
var i = 5; //= int i = 5;
var s = "Hello"; //= string s = "Hello";
var d = 1.0; //= double d = 1.0;
var numbers = new int[] {1, 2, 3}; //= int[] numbers = new int[] {1, 2, 3};

and so on, but we must consider that all of this variables are local variables.

Some of implicitly typed local vaiables restrictions

1 : - it must be initialized during its declaration.
eg ... var x; // Error, no initializer to infer type from.

2 : - the initialization value can't be object or Collection value it can be new expression of objects or collection by using new keyword.
eg .. Var y = {1, 2, 3}; // Error, collection initializer not permitted.
var numbers = new int[] {1, 2, 3}; // Valid

3 : - it can't set to be null during the Compile time.
eg .. var z = null; // Error, null type not permitted.

4 : - we can't use var as a return type of any method.
Public var add ( int x,int y); // invalid.

That’s it.
See you in the Next Part isA

1 comment:

Ahmed Essawy said...

Good Geme , Really I am interesting to this topic because I didn't start with C# 3.0 before .