Welcome to WindowsClient.net | Sign in | Join

Ales Rosina's Another .NET Blog

Description cooming soon :)

New stuff in C# 4.0

I’m just prepering for a presentation of new features in VS 2010 and .NET Framework 4.0, so let me give you just a little sneak peak into new cool features of C# 4.0:

dynamic type

Very short said, it’s something like var in script languages. But let me give you a simple example:
dynamic test = new MyObject();
and even MyObject doesn’t have method (for example) called DoSomenthing(), you can write:
test.DoSomething();
and compiler will compaile with no errors. You would just get an runtime exception, saing that object doesnt have that method.
In real world that can be used for example in looping through some array, wich is defined like object, but you know, that you have in array only strings.

optional parameters

This is best explained through simple example, where I have one method, defined like this:
void DoSomething(int x, int y, int z=5) { … }
And when calling this method, i can leve out z parameter (it will take the default value defined in method) like this:
this.DoSomething(5,3);
And it’s even better – you can mix parameters in calling functions and define parameters by its name:
this.DoSomething(y:3,x:5);

There are some more cool features, especially about Office developement, but you can find that information on the web :) .

I hope you get the picture – more info you can find in this cool video or this blog post where you’ll find some links about other cool fetures in C# 4.0.

Posted: Dec 02 2008, 09:43 PM by alesrosina | with 5 comment(s)
Filed under: ,

Comments

MathieuRoseboom said:

Why is this cool? (dynamic types)

This will lead to more and more runtime exceptions, since the compiler didn't catch them... In my opinion it is definitely something to avoid...

This, and the var keyword aren't really improvements I think. They just make your code look chaotic (you never know what type it is) and it's vulnerable for errors.

# December 5, 2008 7:52 AM

alesrosina said:

Yes, that's true ... But if you ever wanted to do such thing nowdays, you had to take a deep look into reflection, which is (from semantinc point of code) very chaotic.

But yes, i 100% agree with you - with more options, you get more responsibilities, wich can be very tricky :)

# December 9, 2008 5:57 AM

Shane said:

First I also Agree with you 100%.  But if you look at the things that are going on with the languages (C# and VB) VB each iteration is catching up to C# that put in the last iteration, and now C# is putting in the features of VB.

The above 2 examples that are being added are just like the polymorphism of VB, and Optional parameters.  And even though I have to do alot of vb development, and have for many years now, those were always features I tried to stay away from, because I would rather have things strongly defined.  

Loose style development, has allowed developers that just want to get the job done quick (sometimes for good reason) write really bad and not very maintainable code.

I would rather these development efforts be focused on more tools and features that allow us to write better code more easily, not just write code in general more easy.

# January 23, 2009 1:37 PM

alesrosina said:

Well, it's a general human problem - we are lazy :) And with that kind of features, you can do some things very fast and easy, but you can also abuse that kind of features.

And yes, I agree that strict programing is better, this is the main reason I went from VB to C# (back in early days of .NET).

And also, with all this fast developement, there are bunch of "developers", that write crap.

But .... this features are now here (ok, will be this year hehe) and we should use them, when we wuold need them.

# January 23, 2009 3:39 PM

GYOXeFNUm said:

eoTRvx

# April 13, 2009 5:40 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter