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

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

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

  10. 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#.

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

  12. 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
  13. 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!

  14. 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.
  15. 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.