tags:

views:

114

answers:

2

Hi,

I am currently working with .Net 2.0 and have an interface whose generic type is used to define a method's return type. Something like

interface IExecutor<T> {
  T Execute() { ... }
}

My problem is that some classes that implement this interface do not really need to return anything.

In Java you can use java.lang.Void for this purpose, but after quite a bit of searching I found no equivalent in C#. More generically, I also did not find a good way around this problem. I tried to find how people would do this with delegates, but found nothing either - which makes me believe that the problem is that I suck at searching :)

So what's the best way to solve this? How would you do it?

Thanks!

A: 

Just use Object as the type , and return null. That means you might need to write an adapter if you need to call a delecate who does in fact have a void return type

nos
+3  A: 

You're going to have to either just use Object and return null, create your own object to represent void, or just make a separate interface that returns void.

Here's an idea for the second one:

public class Void
{
    public static readonly Instance = null; // You don't even need this line
    private Void(){}
}

that way someone can't create an instance of the class. But you have something to represent it. I think this might be the most elegant way of doing what you want.

Also, you might want to make the class sealed as well.

Joel
Yes, that's what I thought so too... I've been using object and had thought about creating my own Void, but both seemed so ugly I thought it better to ask.Thanks!
anog