Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
×
Microsoft

Anders Hejlsberg on C# 3.0 386

spongman writes "Channel9 has a video of Anders Hejlsberg demoing C# 3.0. The new language enhancements include implicitly typed locals, extension methods, strongly-typed lambda expressions, anonymous types, and LINQ - a builtin SQL-like syntax for data access. The spec, samples and a working compiler can be found on MSDN."
This discussion has been archived. No new comments can be posted.

Anders Hejlsberg on C# 3.0

Comments Filter:
  • by bogaboga ( 793279 ) on Sunday September 18, 2005 @07:04AM (#13588948)
    For those just like myself who might not know who Anders Hejlsberg is, I post a link http://en.wikipedia.org/wiki/Anders_Hejlsberg [wikipedia.org] that might help you understand who this man is.

    I did not know he was very instrumental in developing Pascal, a language I was an expert at one in the mid nineties.

  • by Mr2001 ( 90979 ) on Sunday September 18, 2005 @07:19AM (#13588985) Homepage Journal
    In an implicitly typed local variable declaration, the type of the local variable being declared is inferred from the expression used to initialize the variable. When a local variable declaration specifies var as the type and no type named var is in scope, the declaration is an implicitly typed local variable declaration. For example:

    var i = 5;
    var s = "Hello";

    Can someone explain the point of this? C# is not JavaScript; these aren't true dynamically typed variables, the compiler just assigns a type for you instead of making you do it yourself. I can easily take half a second out of my day to figure out what type a variable should be, and end up with more readable code.
    • by iGN97 ( 83927 ) on Sunday September 18, 2005 @07:27AM (#13589012) Homepage
      You type less, obviously.

      It also allows you to write pieces of code that are more generic. The LINQ samples have a lot of examples of this.

      For example:

      foreach (string s in collection) {
            Console.WriteLine(s);
      }

      means you have to change the type everytime you change the type in the collection.

      foreach (var item in collection) {
            Console.WriteLine(item);
      }

      means that you can use it with any time that implements ToString, which is pretty much any type.

      There are numerous other benefits from the type inference, and they become more apparent with lambda expressions, where you can write expressions like "x => x % 2 == 0" instead of writing the equivalent bool-returning delegate with a typed parameter.

      Most of the new features of C# 3.0 are quite impressive and more importantly very useful.
      • I guess all the implicit typing additions make sense in the context of LINQ, where SQLish queries are translated into C# code involving lambda expressions. In all my years of coding in Delphi and now C#, I've never needed to use BDE, ADO, or any other OOP database interface, so I can't gauge how useful these additions will be.

        However, this example of yours seems like a bad one:

        foreach (var item in collection) {
        Console.WriteLine(item);
        }

        If you're changing the type of the collection, a few for

        • If you're changing the type of the collection, a few foreach loops are the least of your concerns - what about all the code that actually manipulates the items' methods and properties?

          It will fail to compile if it really doesn't make sense. See my other post for comments on the danger of logic errors that remain here.

          And if you're using a snippet like that so much that you're tempted to paste it over and over to work with collections of different types, you should really change it to a generic method

      • Um, C# classes automatically inherit from 'object', which provides ToString.

        So just:
        foreach (object o in collection) {
                Console.WriteLine(o.ToString());
        }

        Or I'm missing something here?
        • If it's a collection of value types (e.g. List<int>), you'll take a performance hit when you convert each item to object. Since Console.WriteLine() has overloads for the intrinsic value types, you can avoid boxing by pulling the item out as an int and passing it to Console.WriteLine(int).

      • foreach (var item in collection) {
        Console.WriteLine(item);
        }


        This example makes no sense.
        Is "collection" a collection of objects or something more concrete? If it's an object collection, s/var/object/ in your example.
        The compiler inferes the variable type at compile-time, not run-time. So "var" will become "object" anyway.

        var i=5; // same as int i=5;
        i="str"; // won't work, because i is an int.

        Of course, maybe I got it wrong..
    • I can easily take half a second out of my day to figure out what type a variable should be, and end up with more readable code.

      Ahh, but just think of all those poor migrating Visual Basic programmers whose only expierience of a c type syntax has been a spattering of javascript. They'll feel right at home.

      With any luck, we'll make programmers of them yet.
    • Can someone explain the point of this?

      One point of it is to allow you to use anonymous classes without having to declare them. Lets say you use LINQ to do a join between two tables - you can assign the result of the join to an implicitly typed variable and reference the fields, without having to give this new object type an actual name.

      So (forgive screwy SQL-like syntax):

      var result = select a.name as name, b.age as age from...;
      log("username is "+result.name+" of age "+result.age);

      The variable result

      • As a Java programmer, it is exciting to see these developments in C#, it makes me wonder whether Java is destined to fall behind C# - it sure looks like that is happening....

        Perhaps. But then again, Java evolved very fast in its first few years, and a lot of what it evolved was crap, particularly the numerous poorly-considered additions to the library that now have to be supported pretty much forever. Policies on language features were made on evangelical grounds -- "We don't need templates!" springs to

    • Can someone explain the point of this? C# is not JavaScript; these aren't true dynamically typed variables, the compiler just assigns a type for you instead of making you do it yourself. I can easily take half a second out of my day to figure out what type a variable should be, and end up with more readable code.

      It's about saving time and typing effort. This C# 3.0 is honestly just a trivial hack though. Why bother with C# 3.0, when there's a .NET language that can be considered advanced enough to be C# 4.0 [nemerle.org]
      • Why bother with C# 3.0, when there's a .NET language that can be considered advanced enough to be C# 4.0; it's called Nemerle.

        Because Microsoft's weight is heavily behind C#, but not Nemerle. Ask any Visual J++ developer how much you can count on Microsoft's long-term support for proprietary languages that aren't their current pet. Caveat programmor. :-)

    • It looks like it would be useful when combined with LINQ type queries as in:

      var q =
      from c in customers
      where c.City == "Seattle"
      select new { c.Name, c.Age };

      q would result in an implicitly typed collection of objects that contain Name and Age properties only.

      Here [msdn.com] is a good description of using both
    • Why implicitly typed variables? Anonymous types.

      Today, you have C# code that looks like this:

      MyClass o = new MyClass();

      Which you could replace with var:

      var o = new MyClass();

      However, with anonymous types, you can have code that looks like this:

      var o = new { Product='Beverage', Price='0.60' };

      Without var, what type would you declare this variable? You could declare it as object, but to access the object's Product and Price properties, you would still need to cast the variable to some type, and w

    • by dotcher ( 761759 ) on Sunday September 18, 2005 @08:06AM (#13589105)
      C# 3.0 also gains anonymous value types. You can do something like:
      var c = new {Name = "Fred Bloggs", Age = 12};
      Now, what type is c? It's an anonymous struct type, with two members - Name and Age. As it's anonymous, it's obviously not possible to declare it using the standard syntax.

      The language is still strongly, statically typed, though - the following would throw a compile-time error:

      c.Dept = "CS";
      Now, as to why this construct was added to the language in the first place: these anonymous types are useful when using LINQ to query a data source:
      var expr = people.Select(p => new {
      p.Name, BadCoder = p.Age == 11
      });

      foreach (var item in expr)
      Console.WriteLine("{0} is a {1} coder",
      item.Name,
      item.BadCoder ? "bad" : "good");
      expr and item are both given the appropiate type for the data they contain, with no need to explicity define the types. Most of the benefits of strong static typing are retained with this approach, and there's no need to define a type to store data for every weird query you run against the database.

      One benefit that is lost is the ability to share these types across assembly boundaries, which might be an issue in real, three-tier applications. On the other hand, if there's a need to pass data directly from the database to the client application, what's the point of having the middle tier in the first place? So, this might turn out to be a non-issue.

      (My examples were taken or adapted from this white paper [microsoft.com], which is an overview of the LINQ project, including the new syntax added to C# 3.0.)

    • Possibly because people keep whining that when typing:

      myClass exampleInstance = new myClass("Test");

      that they have to type the word "myClass" twice.

      This way you can presumably type

      var exampleInstance = new myClass("Test");

      Not sure it's a major saving though...
    • This is incredibly useful when you have generic types in a more complex scenario. Something like this is a better example:

      Iterator<AdvancedHashMap<HashingClass56,NestedKey, ArrayList<int>>> i=
            new Iterator<AdvancedHashMap<HashingClass56,NestedKey, ArrayList<int>>>();

      when definining a complex generic type, not having to declare the full type for "i" makes sense, no?
    • This would be a boon to boost::spirit in C++. In fact, they're thinking of reusing the auto keyword to add this functionality to C++0x.

      Check out this page [boost.org] to see why it'd be so useful.
    • If your C# code ever becomes reasonably readable or maintainable, you can easily be replaced.
  • by hritcu ( 871613 ) on Sunday September 18, 2005 @07:20AM (#13588987) Homepage
    Do we really have to have a Slashdot post followed by a flamewar every time a guy at Microsoft opens his mouth?
    • Equally not meaning to be rude, but some people are interested in more than Linux and OpenOffice. This is supposed to be News for Nerds, and news about version 3 of a language fits right in.
      • My remark was not speciffically about this post, which is pretty OK. I just have the impression that Microsoft-related articles tend to arouse much more animosity then almost any other kind of Slashdot article. And there were a lot of these type of articles lately. I'm starting to become tiered of flaming microsofties, so maybe I will just skip them.
    • Are you a newbie? That is the whole point of this website.

      If you don't want to bash MS, then go to some MScentric site and wax wonderful about their propietary crapola. Some that are on the MS payroll continue to espouse the virtues of MS here (perhaps you?!), but we all enjoy how much time, money and effort MS spends trying to alter the overall message of slashdot.org.

      Lesson for MS to learn: FUD doesn't effect a community of a single mind and purpose.

      C# will not replace Java. It is a wonderful Ja

  • The built in SQL like language would be usefull in many languages. It is now just waiting for LINQ version X (for short LINQX) before the confusion starts.
    • Re:LINQ (Score:3, Interesting)

      by RAMMS+EIN ( 578166 )
      ``The built in SQL like language would be usefull in many languages.''

      Yep. If done right, it can completely eliminate the possibility of SQL injections. This is something that too few people realize, but with SQL injections being a very common kind of vulnerability, it's definitely worth putting some thought and effort in.
  • Way to go! (Score:5, Insightful)

    by RAMMS+EIN ( 578166 ) on Sunday September 18, 2005 @07:48AM (#13589057) Homepage Journal
    It seems that Microsoft is finally doing something with all the great research they funded. C# is becoming more and more like Lisp and ML, and might well become the first language in recent times that carries the approval of both academics and the industry. .NET as a whole gets many things right; multiple languages that can target the CLR and interoperate, special modifications made to create a friendlier environment for functional languages, registration of the core technologies with an independent standards body, publication of an implementation for multiple platforms, with source code, etc. etc.

    Of course, .NET doesn't get everything right, but it's amazing how many good things are in there, especially considering that it comes from Microsoft.
  • PDC Keynote, day 1 (Score:3, Informative)

    by iGN97 ( 83927 ) on Sunday September 18, 2005 @07:51AM (#13589062) Homepage
    For anyone interested in development on Windows, or the possible future direction of Mono, I recommend checking out the PDC Keynote for day 1. The duration is about 3:20, and you'll see demonstrations of the 5219-build of Vista, a lot of Avalon apps, four great architects at Microsoft doing live coding (showcasing LINQ, Indigo (WCF), Avalon (WPF) and Atlas, among other things) and a lot of interesting information about where things are heading.

    A lot of it is market-speak and sales, but it is a developer conference. The link can be found here: http://www.microsoft.com/events/executives/billgat es.mspx [microsoft.com] (Click on the "On-Demand Webcast"-link. High up on the page.)
  • The Microsoft Trap (Score:5, Interesting)

    by MrSteveSD ( 801820 ) on Sunday September 18, 2005 @07:55AM (#13589070)
    C# is a nice language, but the problem is I just don't trust Microsoft anymore. From a business perspective sticking with Microsoft has proven to be a mistake.

    I work for a software house in London and we have a large VB6 application that has been built up over many years. VB6 has effectively been dumped by Microsoft, so our application is slowly rotting away. There is absolutely no way we can rewrite it in C# or VB.Net, we just don't have the resources. I suggested that we at least write all new components in C# and use interop, but that turned out to be a real pain, especially when trying to debug.

    So what do we do? Spend a fortune rewriting our product in C# while our competitors (who may be using Java) continue to improve their products. And once we have eventually finished the rewrite, will Microsoft just dump .NET and move on to something else?

    I have to wonder. If there had been a number of VB6 vendors, rather than just Microsoft, they could never have dumped VB6. In that situation we would have all just moved over to another vendor.

    Is anyone else here in a similar situation?
    • Yup, me.

      Maintaining a big database driven app. But I'm in a somewhat less nasty situation. We have this curious setting here, people keep coming up with new ideas for the program, and I keep implementing them. This is allowing me to simply decide that a change is radical enough so that I might as well rewrite that part in C#, and that's what I'm doing. For now it's going pretty well. Since it's an internal app, making people run two programs instead of one is quite possible.

      It looks like Mono is progressing
    • by callipygian-showsyst ( 631222 ) on Sunday September 18, 2005 @08:13AM (#13589127) Homepage
      Is anyone else here in a similar situation?

      Yes! We has all these Apple Hypercard applications, then Apple dumped us like a hot potato! However, I'm prohibited from the /. "terms of service" from saying anything bad about Apple, or comparing Apple to Microsoft.

      • Hey, I had a ton of stuff written in Dylan! And a big OpenDoc application too! And I've still got to port my accounting system off of Applesoft Basic (hey, wait, didn't Microsoft have their fingers in that one too?)

    • by Anonymous Coward on Sunday September 18, 2005 @08:15AM (#13589135)
      The real problem is that while your competitors were rewriting their products in Java, you were sticking with VB. And now you want to blame Microsoft for that. Sorry, that's not gonna fly. And yes, I am a software developer. Rewriting into another language can get you a lot of benefits that you can quickly roll out into your application. It's not 'just' rewriting. And .NET does not necessarily mean platform/vendor lock, either.

      Not that that actually matters, because most people are all still using Windows anyway, and will be for a long time.
    • Is anyone else here in a similar situation?

      Yes, we had a similar problem, made even worse by the fact we needed something that could scale a lot better than VB does.

      The solution for us was to move to Java. Fortunately our company is growing at a resonably rapid rate so that we were able to adsorb the VB programmers who for one reason or another didn't want to move to Java into some other role.

      We still have a few older customers running the old VB6 application but by end of next year they will all be moved o
  • by Sanity ( 1431 ) on Sunday September 18, 2005 @08:01AM (#13589089) Homepage Journal
    It is exciting to see developments like this in C#, particularly stuff like LINQ (the inelegance of using SQL from within other languages has bugged me for quite some time).

    Java's language features, by comparison to C#, seems to be moving along at a glacial pace, only recently getting features like foreach loops, and generics.

    I personally prefer Java because of Eclipse [eclipse.org], but Sun are really going to have to get a move on if Java is to remain competitive with C#.

    • I'm not sure I'd call it a "glacial pace". There are plenty of enhancements being made to Java (the platform and language) beyond convenience enhancments. See JCP [jcp.org].

    • Java innovates but in a different area. There is a lot of emphasis on extending Java with Aspect Oriented Programming (AOP). There is an incredible extension to Java called AspectJ. You owe yourself to download AspectJ and its Eclipse plugin. You'll see the future of java programming when you try it.
    • by fupeg ( 653970 ) on Sunday September 18, 2005 @11:26AM (#13589856)
      The answer to your question is no. Java has clearly borrowed some things from C#, though C# itself heavily borrows from Java. The EJB 3.0 spec [jcp.org] for example will make use of dependency injection. Take a look at Groovy [codehaus.org] which will be integrated into Java (javax.script [java.net] in 6.0.)

      Now it is true that as a more widely used (on the Enterprsie level) language, Java is going to move more slowly than C#. C# is newer and trying to take mindshare from Java, so it must move faster. Just having a great IDE is not enough.
    • Java's language features, by comparison to C#, seems to be moving along at a glacial pace, only recently getting features like foreach loops, and generics.

      Even worse, C's language features don't seem to have moved since the late '70s. Thank god no-one is still using that any more.

      Phillip.
  • Language Bloat? (Score:4, Interesting)

    by Xepo ( 69222 ) on Sunday September 18, 2005 @08:46AM (#13589239) Homepage
    They keep adding more and more stuff to the language...is there anyone that can really read all of the code that is possible to be written in C#? It sounds like a readability nightmare to me.

    Heck, it took me years to learn all of the components in C++, and I'd bet that the specification for the complete language is now much smaller than C#'s. And there's still stuff waiting to be discovered on the level of template meta-programming.

    Is all of the stuff they keep adding actually useful? Or is it just being added so that they can give the impression of progress, and maybe convert more people to using it? Granted, I'm excited about C++0x, but unlike what I would be thinking if I used C#, I'm not worried because I trust the standards body to not put completely unnecessary stuff into the language. What do you C# programmers think?
    • in about six months, they'll be able to rename C# "perl without the @, or the $"! "P-@$"? They can advertise it as "As powerful as a scripting language, like perl!", and all of the half-intelligent programmers will read that as "Gives you nightmares from trying to read code, just like perl!"
    • Re:Language Bloat? (Score:3, Informative)

      by man_of_mr_e ( 217855 )
      Well, just like the more esoteric features of C++, you don't have to use them if you don't want to. If you want to stick to a subset, you're more than welcome to.

      BUT, for those people that DO want to use them, they provide powerful features that would require either a lot more work, or couldn't be done at all. Or, the features make the language safer, and less error prone for certain tasks.

      Lots of people still write C++ code as "more strongly type C" or "C with classes". They don't use templates, or over
  • by alucinor ( 849600 ) on Sunday September 18, 2005 @08:46AM (#13589241) Journal
    Thanks to some of the brilliant developers at MS, we can get some awesome stuff out of this company, like C#.

    Now, too bad management will keep on trying to tie .NET down to their sinking ship, Windows.

    If only the .NET framework could be freed from Windows, and given the official blessing to target all platforms, including Linux, then Microsoft could set itself up as the supreme tools vendor for the platform. It'd be a bright future for MS, and overnight .NET would probably become the de facto standard for development on Linux!

    But no ... the stodgy petrified boys up top will always put Windows first ... to MS's undoing, no doubt.
  • by ChaoticCoyote ( 195677 ) on Sunday September 18, 2005 @08:51AM (#13589254) Homepage

    Why, oh why must language inventors continue to add every possible concept to their pet project? Must every language try to be everything to everyone?

    No programming language is suited to all applications; anyone who claims omnipotence for their particular language is exhibiting either ignorance or arrogance. A wise programmer knows how to use many tools in appropriate contexts; it's this sort of rational maturity that separates amateurs from professionals. It makes no more sense to develop a web-hosted applet in C++ than it does to write a high-performance batch-processing engine in Java. Using multiple programming languages isn't a simple matter of syntax -- it's a matter of divergent perspectives that force me to think about what I'm developing.

    A disturbing trend has emerged in the last decade, with developers trying to make every programming language applicable to every task; we add object-oriented features to COBOL and Fortran, add generic types to Java, and expand the C++ library with a plethora of complex templates. Now C# is "borrowing" all sorts of ideas from all over the map, without any thought for how all these pieces fit together into a cohesive and logical whole.

    In the end, we get bloated tools that include features ill-suited to their core design. Instead of focusing on a clear set of goals, languages compete in an edless feature competition that often ignores sound engineering practices.

    I have done professional C# programming, and the language does not impress me. Certainly it has some very good ideas -- but it lacks any sense of cohesion in design or intent, and it's ties to Microsoft make me leary of using it for long-term coding projects.

    • by superid ( 46543 ) on Sunday September 18, 2005 @09:14AM (#13589328) Homepage
      I don't think this is a kitchen sink addition. I think this will be a welcome addition to a very large fraction of C# developers. First, my gut feeling is that the majority of non-trivial C# applications connect to databases of some kind, this will help that. Second, even if you don't use a DBMS this will be useful for complex operations on pretty much any data structure that you create.

      This will make me a lot more productive and I'm going to install it ASAP!

      • In C#'s domain, improved database connectivity is very important. Heck, I've been known to write a bit of VB or C# when the situation calls for it -- if I want a quick Windows app, I reach for C#, not C or C++. However, I don't do very much platform-specific work anymore, and Mono notwithstanding, .Net-tools aren't really appropriate to the code I develop.

      • C++ was also built up over time with a continuous chain of "welcome additions." And then the language collapsed under its own weight.

        Isn't the whole point of the .NET environment that it suddenly becomes trivial to get modules written in two different languages to work together? If so, then why isn't it being leveraged by, say, using Microsoft's Scheme.NET (or whatever they call it) for areas where functional makes sense, using C# in areas where imperative makes sense, etc. etc.

        Makes me wonder why they bo
    • Why, oh why must language inventors continue to add every possible concept to their pet project? Must every language try to be everything to everyone?

      This isn't just a random heap of features. They all combine to create the query syntax which can run in-memory as C# code or be translated to external query languages such as SQL or XQuery and sent to a remote server for execution.

      No programming language is suited to all applications;

      But C# with embedded query syntax is an elegant solution to bridging high le
    • A disturbing trend has emerged in the last decade, with developers trying to make every programming language applicable to every task; we add object-oriented features to COBOL and Fortran, add generic types to Java, and expand the C++ library with a plethora of complex templates. Now C# is "borrowing" all sorts of ideas from all over the map, without any thought for how all these pieces fit together into a cohesive and logical whole.

      True, true... *shaking head* all those nice people, if only they knew ho

    • I think there's a lot of truth in what you wrote there. That said, while specialist languages have their place, I'm increasingly wondering why no-one has yet produced the next great general purpose language.

      Why can't we have a simple, elegant syntax like much of Haskell or Python, with punctuation frenzies reserved for things like regular expressions and printf formats where history shows they work well? Why can't we have a grammar that's amenable to parsing by automated tools, to make it easy for IDEs to

  • by XNormal ( 8617 ) on Sunday September 18, 2005 @09:15AM (#13589330) Homepage
    As an open-source I really hate to say this but...

    This is a terrific example of honest-to-god innovation from Microsoft.

    Yes, I know, the building blocks have been available in some form or another in many other platforms. But so far nobody has managed to bring all of this together so elegantly.

    The features are not just a random heap of syntactic sugar. They combine to create the query syntax (using lambdas) which can be either executed directly in C# (with the help of external methods) or be available as a runtime data structure (shades of lisp) that can be translated dynamically to an SQL or XQuery and sent to a remote server for execution. The type inference ensures that the query syntax is not littered by type declarations yet remains typesafe.

    Nice work, Anders. I guess the Comega team deserves much of the credit, but I have the feeling it was Anders who brought it all together into a clean and not too "academically smelling" framework.
  • by Anonymous Coward
    What gives Microsoft the right to unilaterally update the ECMA C# standard? Unless the whole ECMA standard thing was just a farce all along.
  • by alexo ( 9335 ) on Sunday September 18, 2005 @11:05AM (#13589780) Journal

    The problem is that MS strongly ties the language with the IDE.

    That is, whenever C# (or Visual [anything] for that matter) gets new features, we have to buy a new version of Visual Studio to take advantage of it.

    For every developer that may work on a project that will use these new features (and our developers tend to move around).

    I have run into several annoying bugs and limitations in the .NET framework 1.1 and MS's response has invariably been "it is fixed in 2.0". However, to use it, we will have to buy VS 2005. Want a bug fix? Buy the new version! Neat.
    Now C# 3.0 comes along. Will we have to upgrade to VS 200x yet again?

    With these forced upgrades, MS has effectively implemented a subscription model for their software.
    • by caseih ( 160668 ) on Sunday September 18, 2005 @12:51PM (#13590185)
      What are you talking about? The .NET environment and C# environment has always been available as a separate, free download from MSDN. While developing in Visual Studio is nice, you absolutely don't have to. The compiler is a standalone commandline compiler and the runtime environment certainly doesn't have anything to do with Visual Studio. The Mono developers regularly download the latest versions of .NET to compile their test suites and compare the results to Mono's results.

      What forced upgrades are you referring to? I must be missing something here.
  • by ari_j ( 90255 ) on Sunday September 18, 2005 @12:08PM (#13590010)
    Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp.

    These changes to C# bring it closer to Common Lisp, and therefore make it easier to include those ad hoc implementations of it.

The 11 is for people with the pride of a 10 and the pocketbook of an 8. -- R.B. Greenberg [referring to PDPs?]

Working...