Discover and share the best of the web!
Learn more about Digg by taking the tour.
New language features in C# 3.0
codepost.org — Are these features innovative? Maybe not, but for sure they're very cool. Check the article I've written at CodePost about the new C# 3.0 language features.
- 686 diggs
- digg it
- dongiaconia, on 10/12/2007, -2/+4Cool stuff about 2.0 too. Especially the ability to do a value check after a return statement. In 1 the compiler gives you an Unrechable Code Detected error when that happens, not that I have ever accidently written code like that myself of course :)
- Olle, on 10/12/2007, -1/+1Check value after return in C# 2.0? Can you elaborate?
- dongiaconia, on 10/12/2007, -1/+12nd example on his other page:
http://www.codepost.org/view/113 - Olle, on 10/12/2007, -1/+1Aaah, I see.
But that is a property of the yield keyword, right? It is not for normal "returns".
The yield keyword is neat but I was hoping for some support for contract programming that I had missed. - dongiaconia, on 10/12/2007, -0/+1Yes, it is specific to a yield return.
- r3zonance, on 10/12/2007, -0/+1It's not cool at all, it just makes it easier for the LAZY coders, and way more difficult for DISCIPLINED developers.
We already have an equivalent of this in .Net, it's called "Object".
Why do we need another one. It's just another way of breaking the strong-typing.
- dieseltravis, on 10/12/2007, -2/+4I hadn't heard about the new object initializers. That would save me so much time.
The LINQ stuff will definitely be the best additions IMO. Funny that site just barely mentions them at the end. Plus what's with the non-standard color coding?- shakin, on 10/12/2007, -2/+3Could earlier versions initialize values in the object creation statement?
eg.
myobj = new obj("value1", "value2");
The new method seems like an interesting way to initialize arbitrary properties, but unless it automatically uses your setter methods you lose validation or anything else what would normally be done on the data. - dieseltravis, on 10/12/2007, -0/+1You can do that, but you'd have to write all the various constructor methods for that object. The more objects, properties, and permutations of construction you have, the more of a pain it is.
Good point about the setters though. - mcarolan, on 10/12/2007, -0/+1No doubt it will initialize the object first, then invoke the setters you defined, otherwise that would be a major design flaw.
- shakin, on 10/12/2007, -2/+3Could earlier versions initialize values in the object creation statement?
- kyriakos, on 10/12/2007, -3/+4how does that string extension thing exactly work? I mean it doesnt declare anywhere that its the "string" class that its adding a static to.
- fleetskeet, on 10/12/2007, -1/+2I think the "StringExtensions" class may be some sort of go-between, or maybe he meant to name it "String".
- Niek, on 10/12/2007, -2/+5You're wrong. Check this piece of code:
public static string ToMD5(this string s)
Note the "string" in "this string s".
The same way, I can add a function to the int base class, like:
public double string ToDouble(this int i)
{ return (double)i; }
VS IntelliSense will automatically recognize this function as an extension method, so does the C# 3.0 compiler. - fleetskeet, on 10/12/2007, -1/+2I thought it would make more sense to use the partial class feature already in 2.0 to do something like this:
public partial class String
{
public string ToMD5()
}
Unfortunately, that does not work.
- mcherm, on 10/12/2007, -1/+2The object initializers feature is particularly nice. I miss that is MOST languages I use... even languages like Python that are normally very programmer-friendly and expressive.
- NiLeS, on 10/12/2007, -1/+1Pardon my ignorance, but can't you do that already?
i.e., have a constructor
Person(string n, string g, bool a)
{ name=n; gender=g; active=a;}
and then call it: Person fred = new Person("Fred", "unknown", true);
or am I just misunderstanding the new feature - kitzke, on 10/12/2007, -1/+2but now you don't have to write it yourself.
- NiLeS, on 10/12/2007, -1/+1Thanks. That makes sense - I could have used that in a recent program or two...
- SideshowPaul, on 10/12/2007, -0/+1For the person trying to understand what is the point of Object initializers,
Which version is easier to understand when you read it?
// The "old"/regular way
Person p = new Person("Niek", "Male", true ); // Create object
When you read this, you have no idea what the 'true' boolean parameter means so its harder to debug/understand the parameters. Plus, since both "Niek" and "Male" Are strings, are they in the correct order? If you switch them around the code would still compile just fine leaving you to figure out the bug this causes elsewhere in your code.
// New way
Person p = new Person { Name = "Niek", Gender = "Male", Active = true };
This is Much Much clearer. No way to screw up the parameter order and its clear the boolean 'true' refers to an Active field. This is one of the many things I like about Objective-C and Smalltalk and its something most languages should copy IMHO.
-Pablo
- NiLeS, on 10/12/2007, -1/+1Pardon my ignorance, but can't you do that already?
- ZmasteR, on 10/12/2007, -9/+4c# 3.0.....sweet! lol....
- BrainiakZ, on 10/12/2007, -2/+18To be honest, I think that giving the developer the ability to use "var" is just going to lead to bad programming techniques. I personally like to know my datatype and have it set the way it should be. That could only lead to problems later in the code.
The other stuff is cool looking though.- pcgeek101, on 10/12/2007, -1/+2And I thought I was the only one thinking this way ... I like knowing the types I'm working with as well. 'var' is just a bit too generic for my programming style.
- detrate, on 10/12/2007, -1/+6"the compiler will 'replace' the var keyword with int", it's not like it takes away from the actual compiled code. The only problems I forsee are with comprehension from the developer.
- CalgaryTechGuy, on 10/12/2007, -12/+4Yep..."var", it just goes to show that MS knows nothing about real programming.
The rest...whatever - sumrandommember, on 10/12/2007, -6/+7var?!?!? are you kidding me. just how dumbed down are they getting this?
- jmacdonagh, on 10/12/2007, -2/+6Actually, you still maintain the same strong typing. The compiler recognizes the type and doesn't allow you to deviate from it. Also, the intellisense and all that is still intact.
Although it seems kind of "lazy" the new var type was used to implement much of LINQ (which is much cooler than the things the article mentioned). - sonofalink, on 10/12/2007, -6/+1Yeaaah... I've worked with .NET long enough to know that I won't trust Microsoft to guess my variable types. This "var" thing is probably one of the worst ideas I've seen. All it will do is generate a new breed of horrible coders.
- Jeras, on 10/12/2007, -1/+1I thought that 'var' would be a bad feature too from a code readability standpoint. But they also included the Anonymous types feature, which makes the var container necessary. If you enable var for anonymous types, you might as well allow any type to use it.
Then again, you can use the 'object' type in .Net to hold the anonymous type, but then IntelliSense most likely won't be able to help when it comes to the type's fields. - blinks, on 10/12/2007, -1/+4The var keyword is type inference (see http://en.wikipedia.org/wiki/Type_inference ), not weak typing. It's a good thing, in general.
- thewise1, on 10/12/2007, -2/+1agreed. If I wanted to program in VB6 I wouldn't be using C#
- Heavy, on 10/12/2007, -4/+9What we reeeeeeealy need is
System.IO.USB- jmacdonagh, on 10/12/2007, -2/+4Libraries are not part of the language. Use Google. I'm sure there are plenty of articles and libraries that will help you talk with USB devices.
- bluehouse, on 10/12/2007, -2/+1Right on to that
- ZeoLeech, on 10/12/2007, -5/+2Hmm, some pretty cool new features! I'm still a bit apprehensive about managed code as a whole though...
And i agree with BrainiakZ about 'var'. It seems totally unnecessary to me and just adds potentially longer JIT application start-up times. - cmer, on 10/12/2007, -9/+6Here are my thoughts:
var = BAD BAD BAD! .NET is and should remain strongly typed.
Object initializers: cool
Anonymous types: very cool!
Extension methods: Woah! Now that's really very cool! I'd like to have that now!
LINQ: it feels kind of weird to me... maybe I'm just not used to it. we'll see!- jmacdonagh, on 10/12/2007, -1/+11Read above. var does not mess with strong typing. It's not going to be like PERL. ;)
- kitzke, on 10/12/2007, -1/+5The way I understood it, var isn't an unknown type, it's just a declaration shorthand for whatever is on the right. i.e. the compiler turns var s = "foo"; into String s = "foo"; since "foo" is a string. Now is this really useful?
- Jugalator, on 10/12/2007, -0/+3Especially for certain upcoming features like LINQ and anonymous types, it can be.
- r3zonance, on 10/12/2007, -0/+1That isn't at all useful for a strongly-typed language. You might as well just define a string as it accomplishes the same damned thing, with less confusion.
Var serves absolutely no purpose, and has no place in a strongly-typed language. C# will just turn into badly written VB.Net like crud!
- EagleY, on 10/12/2007, -10/+3Ahh yes , they are adding VB features to C#
eventually C# will become VB#
VB rulez :)- lydon, on 10/12/2007, -7/+2I agree, VB is the shiz. VS2005 programming VB is the most productive development environment EVER. Sadly, though, most of the jobs out there today want C# programmers and don't realize that a component written in C# loses nothing when instanced as a VB object, or vice-versa.
PS: I want a digg for developers only...
- lydon, on 10/12/2007, -7/+2I agree, VB is the shiz. VS2005 programming VB is the most productive development environment EVER. Sadly, though, most of the jobs out there today want C# programmers and don't realize that a component written in C# loses nothing when instanced as a VB object, or vice-versa.
- kish, on 10/12/2007, -6/+0Dugg cuz it's cool to learn about, but a lot of these new features are not good to have! As mentioned in the comments, weak typing leads to sloppy programming. And..."extensions"...wow, they added a powerful C++ feature that was left out of earlier .NET frameworks for reasons unknown to developers like myself so that they could later add it and of course give it a proprietary name and syntax...gj microsoft.
- vinny, on 10/12/2007, -0/+3I currently develop in Java and I'm curious why somebody might choose C# over Java. Can you guys give me some examples?
Thanks,- dongiaconia, on 10/12/2007, -1/+3.NET framework comes with windows now, so if your target users are only people running XP (and I believe 2000), they don't need to do anything special to run things that use .NET. With java you have to install the JRE. Some users have difficulty figuring this out.
Other than that they are really very similar. The other nice thing is that despite how much most microsoft products are pretty horrific to have to use, visual studio 2005 is actually really a great development environment, and it is very strongly tied into C#. I'm not sure how well it works visual studio works with Java. - tempusrob, on 10/12/2007, -1/+1True that users need to have the JRE installed, but most people have already anyway, since it was likely needed previously for a website that used a java applet.
- ChanKaiShi, on 10/12/2007, -0/+1Java is refering to both language and framework (Java VM). C# refers just to language (one of many which you can use to write .NET applications). So you should have asked why would I choose to use Java vs .NET for writing applications. This will be a valid question. While I did not program Java and did just standard COM etc stuff in Windows. .NET is leap from anything from what MS did with development in pre .NET enviroment especially coupled with Visual Studio tools. (BTW common misconception that you need to use MS tools to compile .NET assemblies, it's not true, all compilers are coming free with .NET framework). Basically according to a lot of articles development jobs are pretty much equaly distributed between J2EE and .NET for business applications.
- Olle, on 10/12/2007, -0/+3I want to like programming in Java. There are many things around it that I like, such as multi-platform support and competing products in general. But the design of Java is very uneven and patchy in my opinion. The event model has changed, there is an IO package and a NIO (New IO). There is a big set of collection classes, but the first generation were all synchronized and aren't recommended anymore for the most part. Those are only a few examples.
Half of the job learning Java is to learn which one of two or more competing classes to use. And even if you learn which one to use you may have to understand the other class too to maintain old code.
I think this happened because Sun was in a rush, and in the beginning the designs weren't thought through as much as they should have been.
Microsoft started out with huge ambitions for .net, so they have no excuse (and they could learn from Java's mistakes). Yet, they manage to screw things up. Examples of screw-ups are not supporting XML in system.dll, the central library (I didn't think of that myself, those are the words of Anders Hejlsberg I believe it was, but I agree), not having generics in version 1.0 (so the system libraries are forced to use un-typed collections or arrays and now we are stuck with those designs) and introducing a complete new set of collection classes (System.Collections.Generic instead of System.Collections).
And soon the old interface will be superseded by XAML, but that is a whole other can of worms.
Still, in my opinion .net is cleaner than Java. But on the other hand they don't have any excuses to not get it right.
All I want is for library designers to _think_ before they do. - vinny, on 10/12/2007, -0/+1Thanks for the feedback. Can .Net be developed on Linux or OS X? If I want a cross-platform solution, is .Net even an option?
- dongiaconia, on 10/12/2007, -1/+3.NET framework comes with windows now, so if your target users are only people running XP (and I believe 2000), they don't need to do anything special to run things that use .NET. With java you have to install the JRE. Some users have difficulty figuring this out.
- drwh0, on 10/12/2007, -3/+2Most of these "new" features are very disappointing. Most of them lead to bad code and lazyness.
I would prefer having to type out int and String over and over than have to worry about accidenly not quoting a number when I wanted to use it as a String. Sounds like just being lazy to me.
For the Object initializers, I don't think this is a good thing. If you wanted a constructor that takes those arguments, then add one. Again, I don't think this is a good idea.
The Extensions don't look too bad. Looks similar to java's static imports except you actually use them on the string object. - dongiaconia, on 10/12/2007, -4/+2Var is useful for polymorphism. At least I think it is.
- jmacdonagh, on 10/12/2007, -1/+3Actually, I don't think so.
In .NET polymorphism can be obtained by defining a variable as a base class but assigning an instantiated child class to it. - hoopy, on 10/12/2007, -0/+1You can implement polymorphism in C# using interfaces.
- jmacdonagh, on 10/12/2007, -1/+3Actually, I don't think so.
- artificeren, on 10/12/2007, -0/+5All of the features this guy mentions are meant as supporters for the LINQ stuff he just glosses over at the end. The examples he gives are so bad that I wonder whether he actually wants anybody to be impressed...
First of all, "var" and "anonymous types" go hand in hand.
If you create an instance of an anonymous type, how are you supposed to assign that instance to a variable without some type of keyword ( "var" ) that means, "I'm of the type of whatever is initially assigned to me". "var" references are strongly typed to the type of whatever is assigned to them, which is required when you assign an anonymous type but want strong typing ( I.E., not having to use reflection against Object ), as you don't know the name of it at code-time.
So, why would you use an anonymous type? When you are dynamicly creating the type from a sql or xml query in LINQ. Querying data and dotting-over-it within internal business apps is the bread-n-butter of most of the programs written today, and these language features make implementing things much easier. - thechao, on 10/12/2007, -1/+2"var" is a response (the equivalent solution) to the TR1 "auto" keyword (which is being remapped) in C++; it solves the same issue of type-inference of compile-time constructed types (a la DSELs etc.) without having to introduce the keyword "typeof" (which is in G++ ... or it used to be). The example he gave of the use of "var" is not where/how it would normally be used. A lot of people think this is a Good Thing, for example, Bjarne Stroustrup ... who you may have heard of.
- cosmotic, on 10/12/2007, -6/+4var: Yeah, unreadable code!
Object Initializers: Yeah, inconsistent object states!
Anonymous Types: Yeah, unmaintainable code!
Extension Methods: Yeah, unmaintainabe and inconsistent code!
Sounds great to me!- Arevos, on 10/12/2007, -1/+3Adding more power to a language will always yield more ways for idiots to abuse that power. But rather than trying to build an idiot-proof programming language, might it not be better to try to avoid programming with idiots?
- nostgard, on 10/12/2007, -4/+2var doesn't seem at all useful to me.
Also, what the hell good is the yield keyword in 2.0? Maybe I'm stupid but I'm not seeing why they didn't just use the return keyword in place of it??
I kinda wish they would have just added named parameters instead of the object initializer stuff. Or does C# have that now?- fleetskeet, on 10/12/2007, -0/+1The 'yield' keyword confused me at first, too.
- Arevos, on 10/12/2007, -0/+2The yield keyword is used in iterators, and instead of returning a value and ending the function (as the return keyword would do), it returns a value and "pauses" the function's execution. On the next iteration of the loop, the function continues from where it left off.
- y2048, on 10/12/2007, -0/+2See http://www.hutteman.com/weblog/2005/04/26-224.html for an example of how yield can be used in C#, compared to how you'd have to implement the same thing without yield in Java.
- purdo, on 10/12/2007, -2/+4the "var" keyword is going to be abused. Whether it's primary purpose is for generic classes or not every new c# programmer who comes along will be using:
var ms ="dumb";
This is hardly any different to how VB used to handle variants. bad move in my opinion.- thechao, on 10/12/2007, -0/+1please see my comment above; its main use is for automatic type inference of very complex types, such as those generated by the C# equivalent of the Boost Lambda library, etc. (http://www.boost.org/doc/html/lambda.html)
- j_bellone, on 10/12/2007, -1/+1Yeah, well, I guess you haven't used Java.
- r3zonance, on 10/12/2007, -0/+1I haven't used Java, but then C# isn't Java, so it's a moot point.
- Arevos, on 10/12/2007, -1/+0These new features remind me of the JVM based Nice: http://nice.sourceforge.net/
- FZero, on 10/12/2007, -2/+1Why don't they cut right though the chase and rename it to Pyhton C#?
- themeparkphoto, on 10/12/2007, -1/+8Steven Jobs hasn't updated Objective-C in 20 years, and they abandoned their other languages (Hypercard, etc.) Microsoft keeps pushing forward.
Why would anyone criticize Microsoft here?- j_bellone, on 10/12/2007, -0/+5Unfortunately that is how the ball rolls simply because it is Microsoft backing the C# standard. I would like to see Microsoft actually roll out an official compiler for Unix/OSX, but we know that that isn't going to happen. Not saying that Mono isn't great, but Microsoft tools are superb. With that said though, I have never seen the reasoning behind Objective-C when you could just use Java, or a massive library of C++ extensions. I guess it just makes programming on OSX easier, or whatever you want to call it.
I am glad to see that Microsoft is pushing forward with the language though, and I hope to be able to finally start learning this sometime in the near future (school is still teaching C++, as they should, IMHO). Its too bad that we're not seeing the same quickness from the Sun camp over there.
- j_bellone, on 10/12/2007, -0/+5Unfortunately that is how the ball rolls simply because it is Microsoft backing the C# standard. I would like to see Microsoft actually roll out an official compiler for Unix/OSX, but we know that that isn't going to happen. Not saying that Mono isn't great, but Microsoft tools are superb. With that said though, I have never seen the reasoning behind Objective-C when you could just use Java, or a massive library of C++ extensions. I guess it just makes programming on OSX easier, or whatever you want to call it.
- senfo, on 10/12/2007, -0/+1Maybe it's just me, but I don't know this poster from a bum on the street. Where is this information coming from? Can you please provide a source to the C# 3.0 specification?
- pkulak, on 10/12/2007, -0/+1Those look like some really handy features. Isn't C# 3.0 supposed to implement closures as well (not just annonymous methods like in 2.0)?
- danielfe, on 10/12/2007, -0/+6That's correct, var is a shorthand to the left hand side and you *do not* lose strong typing using var. The most interesting reason to use var in my opinion is the ability to dynamically project types. If you've programmed SQL you've done this before, where you have a select statement and you can add columns to it without "hardcoding" your own type (ex: Select Name, Address, ZipCode, from Customers). What var does is it adds that power and flexibility so that you can project your own types without needing to manually create them. The key here is that even though you are projecting your own types, the resulting type from var is strongly typed. How? What it does is automatically infer the type based on what your passing in by inferring the underlying types being assigned (ex: Name field is a string, ZipCode is an int, etc). For more information on var, read my blog post on "what is the purpose of var" - http://blogs.msdn.com/danielfe/archive/2005/09/13/464904.aspx
The real power of C# 3.0 is LINQ (Language INtegrated Query), which enables you to query databases, objects, and xml natively in a general purpose programming language. For more information on LINQ or how we enable strongly typed querying on multiple datasources in a fully extensible way, you should watch this video by Anders Hejlsberg, the "father" of C# (and Delphi and Turbo Pascal) - http://channel9.msdn.com/ShowPost.aspx?PostID=114680
Hope this helps, clarify things and digg-on :) - pennyfx, on 10/12/2007, -4/+1I don't see how the var is going to help at all. How is visual studio's intellisense going to know what type you want when you press "."
there is an entirely different set of methods and properties for a string type ie.
string g = "hello this is lame";
g.EndsWith( )
g.IndexOf( )
than an integer
int k = 45664;
k.EndsWith() doesn't exist
k.ToString() is just about the only worth while method.
If you use
var g = "This is super lame";
then when you try to do
g.WhatEver() how is intellisense going to know what to show you.. Or what methods are even valid.. This doesn't make any sense.. Unless "Intellisense" is now also interpreting what you're placing on the right side of a declaration on the fly.- lancefisher, on 10/12/2007, -0/+0Yes, VS does what they call background compilation.
- bobmcsmith, on 10/12/2007, -0/+5Intellisense (just like the compiler) will see that "This is super lame" is a string and when you type "g." you will get the list of methods for Strings.
- OafTobark, on 10/12/2007, -2/+2The var type is crap. Make the coder init the var in the type he intends to use it for. This leads to a compiler making a determintaion and the point of compilation without the ability to know the intended use of the variable!!
- nicholaides, on 10/12/2007, -1/+1"This leads to a compiler making a determintaion and the point of compilation without the ability to know the intended use of the variable!!"
That's what the variable's name is for. - recursive, on 10/12/2007, -1/+1>That's what the variable's name is for.
No, actually, it's really not. It's a pretty crap compiler that would break if you used the wrong type prefix for your variable names. - Arevos, on 10/12/2007, -0/+2The compiler knows exactly what type is needed at compile time, by calculating the return type of the expression on the right hand side of the assignment operator. Sure, this feature can be abused, but so can any aspect of a programming language.
To me, the var keyword seems a good idea, as it removes redundancy when creating an object. You no longer have to write out the class name twice over. If you want more control, or more checks and balances, you don't have to use var. To an intelligent programmer, this is a good tool that has it's place.
- nicholaides, on 10/12/2007, -1/+1"This leads to a compiler making a determintaion and the point of compilation without the ability to know the intended use of the variable!!"
- sbrown123, on 10/12/2007, -2/+1CodePost keeps deleting my comments. Bastards. I don't think they like people pointing out their joke. So Ill spoil it for everyone here. What CodePost is describing is not future features of C#, but rather capabilities of a prototype based language we like to call Javascript. No digg because its not even funny.
- raisenero, on 10/12/2007, -0/+2There are quite a few complaints about the "var type" (it isn't a type). It's funny that so many people would make judgments about a feature they, apparently, don't understand and complain about its potential to facilitate poor coding habits.
@(The bulk of people complaining about the var keyword)
If you give your code the same level of scrutiny that you give language features, you don't need to worry about bad coders, IT'S YOU! - arrrrrg, on 10/12/2007, -0/+2You guys miss the point on the var thing. If you don't want to use it for your Strings to save 4 keystrokes (and perhaps make things less readable), that's fine. The point is that it is an ESSENTIAL FEATURE if you want to be able to create anonymous class instances without declaring the class type first, which is the next feature that he shows, and IMHO something that could be quite useful. YMMV.
- r3zonance, on 10/12/2007, -0/+1The anonymous class thing, should use the inheritance/polymorphism qualities of OO, and then only affect classes they are intended to.
I mean if you have two possible classes with different methods, you've still got to validate/catch/handle any errors if you use Var or tradition OO methods.
Might as well stick with OO methods as at least it's a bit more clear what the value is expected to be.
VAR IS *****!
- r3zonance, on 10/12/2007, -0/+1The anonymous class thing, should use the inheritance/polymorphism qualities of OO, and then only affect classes they are intended to.
- digason, on 10/12/2007, -1/+1What would be really useful would be fall through in a switch statement. It still doesn't make sense that that's not allowed.
- y2048, on 10/12/2007, -0/+3see http://msdn.microsoft.com/vcsharp/programming/language/ask/switch/
it's there, albeit with a somewhat strange syntax.
It does prevent accidental fallthrough though, which is an easy mistake to make in C++ or Java
- y2048, on 10/12/2007, -0/+3see http://msdn.microsoft.com/vcsharp/programming/language/ask/switch/
- joeyjojo, on 10/12/2007, -0/+1LINQ looks great. Anyone know if that will be added to VB.net as well?
- lancefisher, on 10/12/2007, -0/+0LINQ will be available to VB.NET developers as well, but you will have to add in line continuations at the end of each line.
- readme, on 10/12/2007, -2/+0A lot of script-like constructs. I really do not like the var keyword or see a need for it. A strongly-typed language should not have weak variable definitions -- it's just out of place.
- casiotone, on 10/12/2007, -0/+3public Dictionary<string, Dictionary<string, List<SomeObject>>> dict = new Dictionary<string, Dictionary<string, List<SomeObject>>>();
still think the var keyword is useless?- casiotone, on 10/12/2007, -0/+0In the case I wrote, it just makes it harder to read. If all I've done is initialised the variable like that, it just means more crap to read. using var in that case will be more readable, as I can easily look at the right hand side to see its a Dictionary<.... In other cases like var x = 5; I fully agree the coder should be shot. But when you have to scroll just to see what you're initialising it to, surely something needs to be done?
It is also meant for the LINQ stuff, and anonymous types.
- casiotone, on 10/12/2007, -0/+0In the case I wrote, it just makes it harder to read. If all I've done is initialised the variable like that, it just means more crap to read. using var in that case will be more readable, as I can easily look at the right hand side to see its a Dictionary<.... In other cases like var x = 5; I fully agree the coder should be shot. But when you have to scroll just to see what you're initialising it to, surely something needs to be done?
- jupo, on 10/12/2007, -0/+1I especially look forward to the easy object initialization and implicit type handling in var declarations. These little doses of syntactical sugar can really make programming more enjoyable.
- dupy, on 10/12/2007, -0/+0Just for the record...
VAR != VARIANT(as in VB's Variant-type). In fact, dont look at VAR as a type, think of it like you would "DIM" if you want to compare it to VB in any remotely-possible way.
I can't believe all the crap I'm seeing here about "wow MS is teh sux cuz now c# is going to be loosely-typed." If you knew .net, you'd know that would be the LAST type of feature to add to the framework or to a language.
Nope, the closest you're going to get to VARIANT is OBJECT, folks. Read the article, var has NOTHING to do with types and more to do with implicit declaration.- r3zonance, on 10/12/2007, -0/+1VAR != VARIANT is true, but Var == COULDN'T BE ARSED TO SPECIFY AT DESIGN TIME.
Why bother!
Might as well just specify it. If you change the var declaration value to an integer at a later date then you've got a ***** of recoding to do either way (that, or the app will tank at runtime).
It seems a bit swings and roundabouts. You gain NOTHING and lose CLARITY. Not a very good trade-off in my books.
C# was the best thing to come out of MS, if this happens then it'll undermine the whole spec of C# as a strongly-typed language.
- r3zonance, on 10/12/2007, -0/+1VAR != VARIANT is true, but Var == COULDN'T BE ARSED TO SPECIFY AT DESIGN TIME.
- bigronaldo, on 10/12/2007, -0/+0Are you being serious or were you joking? Just in case, C# 3.0 is real. Microsoft released the specs back in Sept 2005.
http://download.microsoft.com/download/9/5/0/9503e33e-fde6-4aed-b5d0-ffe749822f1b/csharp%203.0%20specification.doc - bigronaldo, on 10/12/2007, -0/+0Oops, my last post was meant for sbrown123.
For anyone interested, the C# 3.0 site is http://msdn.microsoft.com/vcsharp/future/. The CodePost post pretty much just outlines their C# 3.0 Language Specification found on that site (minus the LINQ stuff). They have some cool videos showing the new features. - inrev, on 10/12/2007, -0/+0Ew "var" I'll pass on that one...maybe I'll find use for it.
As for linq...its going to be very cool, a great feature. There is already a MS programming language that lets you query objects, ready for this one....Visual FoxPro!!! You can select from a database into a local (client side) curosr, somewhat similar to a .NET DataSet, and then you can do actual "select" statements on those cursors. Very minimal code, and very fast.
Try getting data out of a dataset in .NET merging it with another dataset and then pulling info out of it based on a whole other set of conditions, can be done, but a lot of code. FoxPro can select and join cursors on the client side. I think LINQ is taking a page from Visual FoxPro, so basically you;ll be able to do "selects", inserts, updates or whatever on dataviews, and dataset, as far as I know. Should be pretty cool. - KevinSmokesPole, on 10/12/2007, -1/+0Ugh, yield is ugly as *****. What's wrong with just doing
public IEnumerable *****(int n)
{
List retval= new List();
for(int i = 0; i < 10;i++)
{
if(i == 3)
return retval;
retval.Add(i);
}
return retval;
}
That yield ***** saves you pretty much ass for time, and looks confusing. They need to drop all this silly ***** and concentrate on linq and framework improvements.
Digg is coming to a city (and computer) near you! Check out all the details on our