Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:20:49+00:00 2026-06-09T16:20:49+00:00

static void HandleDemoEvent(object sender, EventArgs e) { Console.WriteLine(bla-bla); } static void Main(string[] args) {

  • 0
static void HandleDemoEvent(object sender, EventArgs e)
{
    Console.WriteLine("bla-bla");
}

static void Main(string[] args)
{
    EventHandler handler;
    MouseEventHandler mouseHandler;

    handler = HandleDemoEvent; //Line#1 - Compile OK
    mouseHandler = HandleDemoEvent; //Line#2 - Compile OK
    mouseHandler = handler; //Line#3 Compile Error  1   Cannot implicitly convert type 'System.EventHandler' to 'System.Windows.Forms.MouseEventHandler'

}

Both EventHandler delegate type and my own HandleDemoEvent method have the same method signature. Yet Line#2 compiles fine (contravariance at play), but Line#3 fails with failed implicit typecast error. A simple example, and I intuitively understand why the compile error is taking place for Line#3, but I can’t settle it in my mind with some kind of formal explanation. Can anybody please come up with a good understanding to explain the difference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:20:51+00:00Added an answer on June 9, 2026 at 4:20 pm

    Delegate co-/contravariance only works for when you’re building a delegate from a method, as in your first two cases.

    It doesn’t work for when you’re assigning one delegate to another, as in your last case. (Unless you use generic delgates, in which case you can use co-/contravariance on the generic parameters.)

    An EventHandler isn’t a MouseEventHandler – but both can wrap a void method taking an object and an EventArgs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static void Main(string[] args) { Action a = () => Console.WriteLine(MethodInfo.GetCurrentMethod().Name); a(); }
public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
static void Main(string[] args) { int test = 1; resetTest(); Console.Write(test); // Should be
static void Main() { string str; str = Console.ReadLine(); while (str != null)//HERE! {
static void Main(string[] args) { //read in the file StreamReader convert = new StreamReader(../../convert.txt);
public static void main(String[] args) { int i = 0; while(i==0){ System.out.println(Possibilities: R, T,
public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader br1 =
static void Main(string[] args) { List<string> tests = new List<string>() { TypeTester.NonExist, TypeTester.Thing, TypeTester.Fruit,
public static void main(String[] args) { staples[] stemp = new staples[8]; int j; try
public static void main(String[] args) { int [][]shatner = new int[1][1]; int []rat =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.