Writing Truely Generic Code in C#

Bruce Eckles continues his great rant on “weak typing.” What seems to be missed by this discussion is that you can actually write generic code in C# without too much difficulty. Delegates provide us a very nice interface independant way to make method calls. For example:

public delegate void speak(string text);

public class person
{
   public void speak(string text) { ... }
}

public class robot
{
   public void speak(string text) { ... }
}

Now, you can make the robot or the person speak by coding:

robot r = new robot();
speak speaker = new speak(r.speak);
speaker(“say something“);

This is actually more powerful than relying on the semantics (ie. the name) of the method, because of situtations when you have a new class where the name “speak“ doesn't exactly fit for communication:

public class dog
{
  public void bark(string text) { ... }
}

A dog doesn't speak, but it does bark, so now I can write:

dog d = new dog();
speak speaker = new speak(d.bark);
speaker(“say something“);

And everything continues to work.

Published Tuesday, March 23, 2004 7:53 PM by Jesse Ezell

Comments

# re: Writing Truely Generic Code in C#

Tuesday, March 23, 2004 3:27 PM by Darrell
Delegates are highly underrated.

# re: Writing Truely Generic Code in C#

Tuesday, March 23, 2004 3:35 PM by kent
Simple method calls are much more readable than this 'delegate' business which, by the way, is another awkward mechanism to sutisfy a compiler not a reader.

# re: Writing Truely Generic Code in C#

Tuesday, March 23, 2004 3:55 PM by Jesse Ezell
They only seem awkward because they are a new concept.
In practice, the syntax for invoking a delegate is exactly the same as invoking a method, so I don't really see how that is much more difficult to use. Additionally, they are not just compiler sugar, they are fully supported by the .NET framework and give you a lot of things like asynchronous invocation.

In any case, if you really want to have less complex code for your readers, you could easily modify the sample to be this simple:

new speak(dog.speak)("say something");

hmm... that looks amazingly similar to C++ method pointers. Not too cluttered if you ask me.

# re: Writing Truely Generic Code in C#

Sunday, December 10, 2006 12:44 AM by Mister

I love peace! and..your site..)))

# re: Writing Truely Generic Code in C#

Monday, July 30, 2007 6:16 AM by zizo

emerald rings and gold rings www.emeraldring.fora.pl

# re: Writing Truely Generic Code in C#

Saturday, August 16, 2008 1:52 AM by abdimuna

nice concept, keep it up man!

some guys are new to C# Land!

# re: Writing Truely Generic Code in C#

Tuesday, December 02, 2008 1:53 PM by Asina

<a href= bestpre.com ></a>

# re: Writing Truely Generic Code in C#

Friday, December 05, 2008 9:01 PM by Semil

<a href= spiritez.com ></a>

# re: Writing Truely Generic Code in C#

Friday, December 26, 2008 8:53 AM by elexx-oc

<a href= membres.lycos.fr/dertull >zx10r graphics</a>

# re: Writing Truely Generic Code in C#

Sunday, March 01, 2009 12:25 AM by elexx-ra

<a href= http://adultromancefinder.com >chat online</a>

# re: Writing Truely Generic Code in C#

Sunday, March 01, 2009 12:32 AM by Olgunka-vq

<a href= adultspeeddatingfinder.com >dating</a>

Leave a Comment

(required) 
(required) 
(optional)
(required)