Generic, Delegate, void

  • Thread starter Thread starter Aquila Deus
  • Start date Start date
A

Aquila Deus

Hi all!

I found a problem when using generic with delegate:

delegate RT MethodTemplate <RT> ();
delegate RT MethodTemplate <RT, AT0> (AT0 a0);
delegate RT MethodTemplate <RT, AT0, AT1> (AT0 a0, AT1 at1);
delegate RT MethodTemplate <RT, AT0, AT1, AT2> (AT0 a0, AT1 at1, AT2
at2);

The definition is fine, but:

MethodTemplate<void> someAnonymousMethod = delegate () { .... };

gives me """ Keyword 'void' cannot be used in this context """
I know void is not a type, but isn't this inconsistent??
 
Aquila,

Why is it inconsistent? Why would you expect that you can use void with
this? It kind of doesn't make sense to be able to pass void to a Generic,
because you could have easily written the class/method without the Generic
parmameter in order to indicate void is being passed.

What are you trying to do?
 
Nicholas said:
Aquila,

Why is it inconsistent? Why would you expect that you can use void with
this? It kind of doesn't make sense to be able to pass void to a Generic,
because you could have easily written the class/method without the Generic
parmameter in order to indicate void is being passed.

What are you trying to do?

What I want is a set of general generic delegates so that I can easily
declare anonymous functions and use delegates in object members and
function parameters.

Without void in generics I have to give generic delegate/function that
returns void a different name.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Aquila Deus said:
Hi all!

I found a problem when using generic with delegate:

delegate RT MethodTemplate <RT> ();
delegate RT MethodTemplate <RT, AT0> (AT0 a0);
delegate RT MethodTemplate <RT, AT0, AT1> (AT0 a0, AT1 at1);
delegate RT MethodTemplate <RT, AT0, AT1, AT2> (AT0 a0, AT1 at1, AT2
at2);

The definition is fine, but:

MethodTemplate<void> someAnonymousMethod = delegate () { .... };

gives me """ Keyword 'void' cannot be used in this context """
I know void is not a type, but isn't this inconsistent??
 
Aquila said:
What I want is a set of general generic delegates so that I can easily
declare anonymous functions and use delegates in object members and
function parameters.

Without void in generics I have to give generic delegate/function that
returns void a different name.

For example, you can use:
someEvent += new MethodTemplate<object, EventArgs>(myEventHandler);
instead of:
someEvent += new EventHandler(myEventHandler);
which requires you declare a new delegate type for each set of function
parameters and return types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top