107 Comments
- 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. - jmacdonagh, on 10/12/2007, -1/+11Read above. var does not mess with strong typing. It's not going to be like PERL. ;)
- inactive, 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? - 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 :) - 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.
- 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. - Heavy, on 10/12/2007, -4/+9What we reeeeeeealy need is
System.IO.USB - 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. - 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.
- 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?
- 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). - 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? - 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, - 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.
- 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. - 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. - Jugalator, on 10/12/2007, -0/+3Especially for certain upcoming features like LINQ and anonymous types, it can be.
- 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 - 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 :)
- 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. - 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? - 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. - 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.
- 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?
- 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.
- 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. - 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.
- 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.
- inactive, 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! - 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. - 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".
- 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.
- 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.
- 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.
- hoopy, on 10/12/2007, -0/+1You can implement polymorphism in C# using interfaces.
- 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 - 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?
- 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)?
- 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. - 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 *****! - fleetskeet, on 10/12/2007, -0/+1The 'yield' keyword confused me at first, too.
- 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.
- sumrandommember, on 10/12/2007, -6/+7var?!?!? are you kidding me. just how dumbed down are they getting this?
- 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. - 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.
- 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?
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. - joeyjojo, on 10/12/2007, -0/+1LINQ looks great. Anyone know if that will be added to VB.net as well?
-
Show 51 - 98 of 98 discussions



What is Digg?