Looking Forward to Using C# 3.0
8/2/2007 2:15:18 PM
I made a post a little while ago detailing the headaches I had trying to install Orcas CTP on my Vista machine (Home Premium). Since it isn't safe (yet) for me to run 2005 / 2008 on the same machine (Virtual PC) I will have to limit my C# 3.0 use to reading about the cool new features. I might have to upgrade to Vista Business Premium before I am even able to Debug in Visual Studio 2005 as there is NO workaround to allow this functionality. Anyway. C# 3.0. Take this Foo class below.
public
class Foo
{
private string _fooString;
private int _fooInt;
public string FooString
{
get { return _fooString; }
set { _fooString = value; }
}
public int FooInt
{
get { return _fooInt; }
set { _fooInt = value; }
}
public Foo()
{
}
}
Using C# 2.0 you would set the values of the properties like this:
Foo foo = new Foo();
foo.FooString = "string";
foo.FooInt = 1;
Simple enough. With a C# 3.0 it is a little more graceful:
Foo foo = new Foo { FooString = "string", FooInt = 1 };
Still simple enough. You could do something similar in C# 2.0 but this would involve overloading Foo's constructors.
public Foo(string fooString, int fooInt)
{
_fooString = fooString;
_fooInt = fooInt;
}
Foo foo = new Foo("string", 1);
While this is a similar approach, it does involve writing more code. Personally I don't mind writing overloads but the fact that I will not HAVE to will be nice.
Extension Methods
Extensions methods are going to be nice too. I first started reading about these a couple of months ago when looking up info on how to combine 2 Dictionary<K, V> into 1*. In C# 2.0 you have to derive from a type and override the methods. Not anymore.
public
static class FooExtensionMethods
{
public static string GetFooInformation(this Foo foo)
{
return string.Format("{0}, {2}", foo.FooString, foo.FooInt);
}
}
To use:
Foo foo = new Foo();
foo.FooString = "string";
foo.FooInt = 2;
Response.Write(foo.GetFooInformation);
*I can't find the link right now but I read a great article a couple of months ago about this guy creating a custom extension methods for Dictionary<K, V>. The method extension that stood was Dictionary.Combine. I will post it when I find it because I want to read it over again!
I am really looking forward to LINQ and will make posts about that in the coming days / weeks once I familiarize myself with it a little more.
Links
C# 3.0: An Introduction
LINQ: Granville Barnett
.NET,
C#
