Architect or Cobbler?
Good code starts with good design

New C# Features

Thursday, September 15, 2005
It's pretty tough sitting at the back here trying to blog this live, I need to improve my typing skills. The first thing, which won't be a surprise after reading my last blog entry is that you don't need to explicitly type local variables:
var i = 773;
var s = "A string";
  Using type inference the compiler can infer the types. It should come as no surprise that these implicit types must always be initialised when used. So var x would cause a compiler error. Extension methods are static methods which can be used to extend the functionality of a class without having to subclass the class, to declare and use this extension looks something like this:
public static class MyExtensions
{
  public static string Foo(this string str)      
  {
    . . .
  }
}

public class Bar {

   public static void Main() {
     string s = "Hello";
     s.Foo();
   }   
}
  Lambda expressions are introduced into the language to allow you to write anonymous methods. I know many of you might not even have got to grips with writing anonymous methods yet, but it does seem like a nice way of writing a piece of inline code. A lambda expression should be familiar to the Lisp programmers among you (or Scheme, or Haskell or any of the other functional languages), it is essentially an anonymous function where values can be bound to the function arguments at run time (or compile time in some languages, I was going to ask, but bottled it)

var match = job.FilterBy((string description, int priority) => 
                         description == "C# Debug" && priority < 4);
  would run a query and return all those jobs that had a description "C# Debug" and priority less than 4. You can also, although I was getting lost by this time (so I know what my homework is), use the lambda expression to build expression trees. C# also includes the notion of initialisers now, so I could write a class and construct it like this:
public class Job {
   string description;
   int priority;
   \\ If these are private then you must provide public Properties
}

public class Bar {

   public static void Main() {
      new Job("C#",4);
   }
}
  If you read my last blog you would have seen me use an anonymous class in Visual Basic and probably wondered what it was. Well C# has the same facility, I simply provide initialisers to the constructor and an anonymous class with appropriate properties will be created. There is a naming scheme for these classes but I couldn't keep up.


   var x = new(description="Fred",priority=7);
  So quite a few new features to be mulling over. It's a long time till C#3.0 is released, so the final release may look nothing like this, although that's unlikely the team appear to be quite a long way along this road, having released pre-beta implementations that actually do this stuff. My head hurts now, but only one session left, and then I can go play on some rides.

# posted by James @ 12:23 AM   3 comments Comments: Variables declared with var. Methods that you can attach to objects without sub-classing.

This looks like javascript to me :-)
# posted by Anonymous Anonymous @ 9:45 PM   It does doesn't it :-)

There are some safeguards though. Once the variable has been 'set', then that type is bound to the variable, so type safety is still preserved.
# posted by Blogger James @ 9:50 AM   oh i can still see myslef getting caught out between

var x = 1;
var x = 1.0;
var x = 1.0f;
var x = 1.0d;

:-)
# posted by Anonymous Anonymous @ 7:27 PM   Post a Comment

<< Main blog page
This page is powered by Blogger. Isn't yours?