Anonymous Types In C# Are A Crippled Hack

I’ve been learning a bit of Java recently, reading Unlocking Android and playing with the Android SDK to try and learn how to write apps for my Droid. I have known, intellectually, about some of the key differences between .NET and Java for quite some times now – how Java doesn’t have properties, how events are done through an Observer pattern, and various other differences. Now that I’m trying to put some of these differences in practice, though, I find some of them to be very intriguing – especially the way events are handled.

Most of the sample code that I see with event handling in Android apps look like this:

   1: someButton.setOnClickListener(

   2:     new OnClickListener(){

   3:         public void onClick(View v){

   4:             //do something here to handle the click

   5:         }

   6:     }

   7: );

</div> </div>

The setOnClickListener method expects a parameter of type OnClickListener with a method called onClick(View). Rather than declaring this as a full fledged class definition and instantiating it, we are using an anonymous type with inline instantiation. We are new’ing up an OnClickListener class and declaring the methods of the class instance inline with the object initializer.

Now look at what we have to do in C#, by comparison:

   1: public void SomeMethodSomewhere()

   2: {

   3:     someButton.SetOnClickListener(new MyClickListener());

   4: }


   6: public class MyClickListener: OnClickListener

   7: {

   8:     public override void OnClick(View v)

   9:     {

  10:         //handle the click here

  11:     }

  12: }

</div> </div>

(Please don’t start complaining about my C# example not using the .NET event system saying that we don’t need anonymous types because we have delegates and multi-cast delegates. I’m well aware of delegates and the .NET eventing system. Honestly, I’m very happy with the .NET eventing system. It works very well and is easy to use. I show this example because it illustrates an apples-to-apples implementation difference between Java and C#.)

The difference may be minor in this small example but it can be rather tedious to have to declare a new class for every single OnClickListener that we want to use. Even if we make OnClickListener an interface, such as IOnClickListener, we will have the same problem and have to declare a new class and implement that interface.

The problem as I see it is that anonymous types in C# are a crippled, lame hack that were only put in place to support the functionality needed for LINQ queries. We can’t declare methods on our anonymous types or instantiate an anonymous type that inherits from anything other than the default Object in C#. This severely limits the capabilities and possibilities of we can do with anonymous types. Honestly, other than ASP.NET MVC Routing declarations and LINQ queries, I have yet to see anyone use an anonymous type in C#.

Dear Microsoft,

Please give us full fledged anonymous types in .NET/C# instead of the crippled hack that was put in place to support LINQ. I would especially love to instantiate an interface as an anonymous type:

   1: public void PleaseLetMeDoThis()

   2: {

   3:     SomeMethod(new ISomeInterface()

   4:     {

   5:         public void DoIt()

   6:         {

   7:             //do something here.

   8:         }

   9:     })

  10: }


  12: public void SomeMethod(ISomeInterface something)

  13: {

  14:     something.DoIt();

  15: }

</div> </div>

You’ve given us anonymous methods with delegates. You’ve given us read-only properties in anonymous types. It wouldn’t take that much work to marry the two concepts into a nice syntax like this.



Compact Framework: Global Hotkeys