[Israel.pm] C#3 bits

Jonathan Worthington jnthn at jnthn.net
Wed Nov 12 15:05:40 PST 2008


Hi,

I've probably written as much C# as anything other language in the last 
year. C# is a huge improvement over previous versions.

Shmuel Fomberg wrote:
> They also have anonymous classes. writing:
> var p1 = new { Name = "Shovel", Price = 495.0 };
> Will create an anonymous class. I still don't know what it's good for, 
> because as much as I seen, the fields of this class can not be accessed 
> without reflection.
>   
It was added mostly for Linq, where it is rather useful. However, there 
are other situations they are useful too; since when you databind a 
collection to a control of some kind in an ASPX page you get late 
binding, you can just build an anonymous class of the data you want to 
bind and it works out nicely. You can access their fields within the 
scope of their "var" declaration without reflection too, BTW.

> It is possible to declare a variable as "var", like this:
> var x = new Customer();
> And x will automatically be of type Customer. cute.
>   
After a bunch of

Dictionary<int, List<string>> Blah = new Dictionary<int, List<string>>();

Mess where you had to repeat yourself so much, this was oh so welcome.

> And one last, there are Extension functions. if declared:
> public static class E
> {
>     public static void F(this object obj, int i) { }
>     public static void F(this object obj, string s) { }
> }
> then every instance of any type, it is possible to class:
> a.F(5);
> and "a" will attached to "obj", i=5.
> Here again, I don't know what it's good for. one day.
>   
Again, added for Linq on the one hand. To me, if you were writing 
extension methods into your code base every day it would probably be a 
sign of a maintenance headache in the making. However, the ability to 
add a method to any class that implements a particular interface, or on 
sealed classes, can now and then be incredibly useful. I've found the 
odd good use of them, and when I've found these cases it's allowed some 
nice architecture, IMO.

C# 4.0, with dynamic typing, is going to also be rather interesting. :-)

Jonathan



More information about the Perl mailing list