Jump to content
  • Advertisement
Sign in to follow this  
remigius

[.net] New features in C# 3.0 in 7x5 mins (and some discussion)

This topic is 4499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry if this has already been posted before, but it seemed very useful [smile] Sahil goes over the new features in C# 3.0 briefly and highlights various uses. You can find his blog over here with some more widom and these posts on C# 3.0: Part 1: Implicitly Typed Local Variables "var" Part 2: Anonymous Types Part 3: Extension Methods Part 4: Lambda Expressions Part 5: Object and Collection Initializers Part 6: (LINQ) Query Expression Translation (to C# 3.0) Part 7: Expression Trees More information on C# 3.0 is available over here on MSDN. I'm still reading through this stuff, but I have a question on the first parts of Sahil's overview. Though I can see the benefits of the implicit typing and anonymous types, doesn't this open a huge door for messy coding ala javascript? The Extension Methods thing also contributes to this. If I want to do:
int i = 10 ;
i.SpankMonkey() ; // Spanks the monkey per the logic you wrote.
Shouldn't I be using another type than an int to hold this value anyway? Extension methods seem to be a cheap way of getting away with sloppy design (the other features too btw). The SpankMonkey method (or the DoubleMe method that makes more sense) aren't conceptually relevant to the int type. If they were, these methods should have been there in the first place. The right course of action would be to either create a new type that subclasses the type if I'd want to extend its functionality (in a conceptually fitting way) or to use it as a member of another type if I just need it to hold some data, that I want to modify in a way that doesn't make sense in the existing type's context. Also, with the anonymous types it seems to be very easy to create a truckload of anonymous class, which sounds like a great deal of unnecessary overhead. For example:
for (int i = 0; i < 100; i++)
  var monster = new {hair="black", skin="green", teethCount=i};
Would this create 100 new anonymous classes, or is the C# 3.0 compiler smart enough to reuse the same anonymous class for these 100 instances? I can't imagine it isn't, but I thought I'd doublecheck.

Share this post


Link to post
Share on other sites
Advertisement
Those look like some nice summaries of the new features. Thanks for the links! For your question, the C# 3 spec says that there will only be one type:
Quote:
Within the same program, two anonymous object initializers that specify a sequence of properties of the same names and types in the same order will produce instances of the same anonymous type. (This definition includes the order of the properties because it is observable and material in certain circumstances, such as reflection.)
In the example
var p1 = new { Name = "Lawnmower", Price = 495.00 };
var p2 = new { Name = "Shovel", Price = 26.95 };
p1 = p2;
the assignment on the last line is permitted because p1 and p2 are of the same anonymous type.
It's occurred to me that LINQ looks a lot like list comprehensions in some other languages, with the added bonus of the orderby keyword. It'll be nice to have.

Share this post


Link to post
Share on other sites
I agree that Extensions are kind of cheap. But I don't think they were intended to be used on types like integers. Perhaps it would be for adding a functionality to a type that is written in another library? Without having to derive from this type, you can simply add functionality to it. However, I wonder if it's possible to access members of the type at all.

Share this post


Link to post
Share on other sites
The implicit typing should be resolvable at compile-time, so you should receive notification of something wrong.

Pipo DeClown, that is exactly how I use it in javascript with expando classes. It's actually rather nice, I've used it to make a few tools here at work that greatly enhance the ease of creating dynamic web apps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!