The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Why do delegate arguments have to match exactly?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eric Gunnerson

Posts: 1006
Nickname: ericgu
Registered: Aug, 2003

Eric Gunnerson is a program manager on the Visual C# team
Why do delegate arguments have to match exactly? Posted: Mar 12, 2004 8:23 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Eric Gunnerson.
Original Post: Why do delegate arguments have to match exactly?
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Gunnerson
Latest Posts From Eric Gunnerson's C# Compendium

Advertisement

Michael asks,

Why do delegate arguments have to match exactly, when creating a delegate using Delegate.CreateDelegate?

Example:

delegate void SpecialEventHandler(object sender, SpecialEventArgs e);

Handler function:

void SpecialMethod(object sender, EventArgs e)
{
}

Now the following

Delegate.CreateDelegate(typeof(SpecialEventHandler), o, SpecialMethod)

fails, even though EventArgs is a direct parent of SpecialEventArgs. Shouldn't it be possible to use (safe downcast?) this method as a delegate target?

This is an interesting question. It should be possible to be able to support this, but we don't support it currently. IIRC, there's a runtime rule that says that a delegate must match exactly. The fancy name for this is delegate contravariance.

This could be something that we support in the future.

Read: Why do delegate arguments have to match exactly?

Topic: Sharepointcustomization, RSS v Atom Previous Topic   Next Topic Topic: Mortal Sin: Translating technical terms

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use