Slashdot Mirror


Anders Hejlsberg on C# 3.0

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."

28 of 386 comments (clear)

  1. Who is Anders Hejlsberg? by bogaboga · · Score: 5, Informative
    For those just like myself who might not know who Anders Hejlsberg is, I post a link http://en.wikipedia.org/wiki/Anders_Hejlsberg 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.

    1. Re:Who is Anders Hejlsberg? by Detritus · · Score: 3, Insightful

      Niklaus Wirth designed Pascal. Anders Hejlsberg wrote a number of popular Pascal compilers, such as Turbo Pascal.

      --
      Mea navis aericumbens anguillis abundat
  2. Re:Looks more like Delphi every release by ergo98 · · Score: 5, Insightful

    But then, Bill Gates himself said that the only thing wrong with Delphi was that it wasn't a Microsoft product.

    What are you talking about? I used Delphi for years, and then switched to .NET, and while Anders of course brought a lot of Delphi-ism to the .NET Framework and C#, these new C# 3.0 additions are nothing like Delphi, and C# 2.0 is already worlds beyond what Delphi ever achieved. LINQ, and DLINQ, are very exciting improvements in removing the disconnect between the database and the middle/front tier, and given the tremendous importance of that it will be remarkable.

    The toughest thing about this sort of technology, though, is that it isn't complete and usable in real projects, so as developers we're uncertain how much time to waste playing with the demos and learning (how many developers must be pissed seeing the hype machine starting over C# 3.0, when they still don't even have the ability to use C# 2.0 in production - e.g. VS.NET 2005 is only at the RC stage). Unless you're a blogger or writer making money writing about how much it makes you wet your pants, there's just no practicality in it.

  3. Re:Looks more like Delphi every release by Haeleth · · Score: 4, Informative

    Er, Delphi never supported type inference, closures, or metaprogramming of the sort that Microsoft are introducing to .NET in C# 3 and VB 9. These features are coming from the functional programming world, from languages like Lisp and ML.

    For existing languages that offer similar features in a braces syntax, see Nemerle or Scala.

    (Languages like Ruby offer related features, but their lack of static typing means they're more distant cousins.)

  4. Why implicitly typed locals? by Mr2001 · · Score: 5, Insightful
    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.
    --
    Visual IRC: Fast. Powerful. Free.
    1. Re:Why implicitly typed locals? by iGN97 · · Score: 5, Insightful

      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.

    2. Re:Why implicitly typed locals? by RAMMS+EIN · · Score: 3, Insightful

      In the past, I've made the argument that forcing people to declare types lowers productivity and decreases legibility.

      The one good rebuttal I got was that it really helps to declare types on function parameters; it serves as a kind of documentation of the intended use and operation of a function. Plus, of course, it allows the compiler to catch errors where the function you write doesn't have the type you say it does.

      I guess that's also the reason Haskell programmers like to declare the types of their functions, even though it's optional in Haskell.

      --
      Please correct me if I got my facts wrong.
    3. Re:Why implicitly typed locals? by Mr2001 · · Score: 5, Insightful

      Having a declared type lets you know how you can use that variable. Can you pass it as a ref parameter to method XYZ that wants a "ref int"? Can you use it to store the result of method ABC, which returns a float?

      In a dynamically typed language, the answer is always yes because variables have no fixed type. But in C#, the variables still have fixed types; they're just hidden. You have to look at the declaration "var x = 5" and think "Hmm, I guess that's an int", just like the compiler does. And for declarations like "var y = SomeFunction()", you have to go look up SomeFunction to find out what type it returns before you can know y's type.

      It might save you a split second of typing to write "var" instead of a real type name, but 6 months from now when you have to find a bug in that code, it'll cost you just as much time to figure out what type those variables are.

      --
      Visual IRC: Fast. Powerful. Free.
    4. Re:Why implicitly typed locals? by dotcher · · Score: 5, Informative
      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, which is an overview of the LINQ project, including the new syntax added to C# 3.0.)

  5. Not trying to be rude, but ... by hritcu · · Score: 4, Insightful

    Do we really have to have a Slashdot post followed by a flamewar every time a guy at Microsoft opens his mouth?

    --
    If you don't fail at least 90 percent of the time, you're not aiming high enough. (Alan Kay)
  6. Re:Looks more like Delphi every release by ergo98 · · Score: 5, Interesting

    One area of intensive research in IT for years is setting up a portable high-performance disconnect between database and other tiers.

    It is high-performance. Watch the demo - at one point Anders sets up a log, and you can see that the LINQ query was transformed into the appropriate, performant T-SQL which is passed to the RDBMS. It isn't doing the standard, shittacular "pull everything back in a terribly unscalable manner and then filter it in the middle tier", but rather appears to be analyzing the whole of the query and communicating it effectively to the source.

    Embedded SQL in computer languages has been around for a very long time

    It isn't embedded SQL. It's set operations that obviously share commonalities with SQL, but are largely different. Again, have you watched the video or read the spec? DLINQ, by the way, is the ORM system that makes the objectpersistence "transparent" (leaky abstraction, like all ORMs, but still).

    Also, I can't believe that MS C# is going to include support for MySQL, Postgresql etc, like Hibernate, NHibernate, JDO etc.

    I doubt it'll include support either, out of the box. Instead, like always, they've created a generalized data services layer that any provider can plug into - create a ADO.NET 3.0 data provider for MySQL, and your data service can be the target of LINQ operations.

  7. Way to go! by RAMMS+EIN · · Score: 4, Insightful

    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.

    --
    Please correct me if I got my facts wrong.
  8. Re:Looks more like Delphi every release by bheer · · Score: 5, Informative
    Er, it might help if you actually read the spec. This isn't 'embedded SQL' in the sense of Pro*C -- the 'queries' are really a bit of (helpful) syntactic sugar over an object-oriented, typesafe set of expressions (you'll see the lambda expressions, new to C#, used heavily here):

    from c in customers
    where c.City == "London"
    select c.Name
    turns into

    customers.
    Where(c => c.City == "London").
    Select(c => c.Name)


    Of course, if you don't like it you can always pass strings of SQL text to the data layer, or do everything with StoredProcs -- after all, DLINQ helpfully shields you from ADO.NET but nothing stops you from using ADO.NET either directly or through alternate layers like NHibernate.

    This should also answer your point about optimised SQL generation ... the programmer does not type SQL into C#, SQL gen is still done in the background.

    Also, I can't believe that MS C# is going to include support for MySQL, Postgresql etc, like Hibernate, NHibernate, JDO etc.

    They don't have to. Implementing DLINQ is really as simple as implmenting a pattern (which Helsberg called the 'query expression pattern') and adding your own DB-specific code.

    Currently Oracle and DB/2 ship libs for ADO.NET, you can be quite sure they'll ship libs for DLINQ. If the MySQL and Postgres communites want DLINQ support badly enough, I'm sure someone will write it.
  9. PDC Keynote, day 1 by iGN97 · · Score: 3, Informative

    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 (Click on the "On-Demand Webcast"-link. High up on the page.)

  10. The Microsoft Trap by MrSteveSD · · Score: 5, Interesting

    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?

    1. Re:The Microsoft Trap by callipygian-showsyst · · Score: 3, Funny
      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.

    2. Re:The Microsoft Trap by Anonymous Coward · · Score: 5, Insightful

      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.

  11. Is Java falling behind? by Sanity · · Score: 4, Insightful
    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, but Sun are really going to have to get a move on if Java is to remain competitive with C#.

    1. Re:Is Java falling behind? by fupeg · · Score: 3, Insightful

      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 for example will make use of dependency injection. Take a look at Groovy which will be integrated into Java (javax.script 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.

  12. Re:Not really by GnuVince · · Score: 4, Informative

    You got it wrong. C# is strongly and statically typed, while Ruby and Python are strongly and dynamically typed. Get your definitions right, dynamic vs static refers to when type checking is performed, at compile-time or a run-time. Strongly typed vs weakly typed means whether you can freely mix types. C, PHP, TCL are weakly typed, Lisp, C# and Ruby are strongly typed.

  13. Written in both-- by Tominva1045 · · Score: 4, Insightful

    As someone who has served as a consultant and written applications in both languages this is what I can add:

    My VB apps were much quicker to prototype- to provide something to a customer in helping them determine requirements when they aren't really sure what they need. Also, the VB apps are often easier to maintain (less cryptic code can be easier to maintain).

    My C# apps were usually written in that language because the client had a preference for it- not that any logical reason was given (unless you count putting C# in marketing information to justify higher costs). Also, because the lanugage can be more difficult to follow (meaning less likely the client's in-house developers can code or update it) than VB, consultans can often charge a higher hourly rate to code in it.

    Both languages build to the same IL output.

    Comments such as this one from above a great language for people who don't want to know to much about what thier program is actually doing - but VB .NET is, and will always remain a hack.

    show a real bias whilst not providing any detail whatsoever. So take them with a grain of salt. Unless your reading machine language, you don't really know what your appp is doing.

    It is possible to write great code in each. It is also possible to write really bad code in each. It comes down to the developers ability and preferences.

    If you have a nice fat contract and want to perch in your ivory tower looking down upon the commoners, go with C#.

    If you have demanding clients and need to give yourself some breathing room, go with VB.

    --
    Cogito Ergo Sum
  14. Language Bloat? by Xepo · · Score: 3, Interesting

    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?

  15. Where's the Kitchen Sink? by ChaoticCoyote · · Score: 5, Insightful

    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.

    1. Re:Where's the Kitchen Sink? by superid · · Score: 4, Insightful

      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!

  16. Microsoft and innovation by XNormal · · Score: 5, Insightful

    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.

    --
    Stop worrying about the risks of nuclear power and start worrying about the risks of not using nuclear power.
  17. Solid evidence, please? by Anonymous+Brave+Guy · · Score: 4, Insightful

    Do you actually have any solid information to support your claims here, or are you just expressing your personal opinion as fact?

    Almost all of the posters objecting to type inferencing here quote examples like your

    var y = SomeFunction()
    as an illustration of how code is less readable without a type. Guess what? Code isn't very readable if you call your functions SomeFunction anyway, and writing
    int y = SomeFunction()
    or
    SystemLibrary.MathModule.FloatingPoint.LongDouble y = SomeFunction()
    really doesn't help.

    On the other hand, if your functions and variables have meaningful names, you probably don't care whether the value returned is a float or a double most of the time. Type inference makes your code more generic, without losing any particularly useful information (if it is useful, you can still specify it), and the type safety of the system prevents the sort of

    double -> float -> loss of precision
    errors typical in languages like C.

    In other words, I see a lot of scare-mongering in this thread, but very little evidence that it's justified, other then people saying, "It's unfamiliar and I don't like it". Do you have anything more for us?

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  18. Re:I have just one problem with it by caseih · · Score: 3, Interesting

    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.

  19. Re:Not really by Coryoth · · Score: 3, Informative

    So because the function says it takes an integer, and I fed it some integer, everything is going to be fine by definition? I think not. You are still going to need to exercise every line of code in runtime tests.

    Static types are just a very weak form of formal specification. A stronger form would be something like contracts (preconditions, postconditions and invariants) as used in things like Eiffel and D and SPARK, which allow you to specify not just the type of the inputs and outputs, but specific properties that are required of them. Another step up the formality chain again requires you to be very explicit in your specification allowing the contracts to be statically checked prior to compilation - that covers all the code paths and doesn't require runtime checking - and properties of the software to be formally proved.

    Static typing finds maybe 10% of the possible potential errors in your program at the expense of doubling the effort to write and understand it. In some cases static typing may be a necessary evil to attain the required execution speed, but as a "safety" feature any benefits it provides are outweighed by the complexity it adds to your code.

    Whether the benefits you gain from any level of formal specification, be it static typing or full formal methods, are worth it depends on how badly you need to be sure the system works and how costly any errors are. For some systems it really isn't that important - having a prototype or working code is more important that catching every last glitch. For other systems (security, avionics, banking and finance software etc.) any sort of glitch, bug or exploit is sufficiently costly that the extra effort to catch as many errors statically as possible is going to be worth it. There is also a spectrum of different needs in between.

    As another note, static types needn't complicate your code as much as you might imagine. Try looking at a language with good static types (SML or Haskell come to mind) and you'll see that static types can be quite easy and require very little extra work (in comaprison to static types in other languages).

    In my experience static vs. dynamic typing is really a matter of how well I've managed to nail down exactly what sorts of data structures I'm going to use. If I'm still doing exploratory/prototype/research code then dynamic types win out every time because they provide incredible flexibility. If I have nailed down what data structures I want to use then static types aren't any extra work and provide and extra buffer of safety.

    Jedidiah.