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

  • SEARCH
  • Home
  • 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 467183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:33:18+00:00 2026-05-12T23:33:18+00:00

Suppose I have code segment namespace Test { public delegate void StaticticsDelegate(object sender,EventArgs e);

  • 0

Suppose I have code segment

namespace Test
{
    public delegate void StaticticsDelegate(object sender,EventArgs e);

    class DynamicDelegates
    {

        static void Main()
        {
            StaticticsDelegate del = BowlerStatistics;
            BallThrownEventArgs be = new BallThrownEventArgs(90, 145);
            del(DynamicDelegates.Main,be);

            del = BatsmanStatistics;
            RunsScoredEventArgs re = new RunsScoredEventArgs("Skeet", 55);
            del(DynamicDelegates.Main, re);
            Console.ReadLine();
        }


        static void BowlerStatistics(object sender, BallThrownEventArgs e)
        {
         Console.WriteLine
         ("{0} sender is ; Bowler Statistics :speed {1} angle {2}",
          sender, e.Speed, e.Angle);
        }

        static void BatsmanStatistics(object sender, RunsScoredEventArgs e)
        {
            Console.WriteLine
            ("{0} sender is ; Batsman Statistics :name {1} runs {2}",
            sender, e.PlayerName, e.Runs);
        }

    }

    class BallThrownEventArgs:EventArgs
    {
        int angle;
        int speed;
        public BallThrownEventArgs(int angle, int speed)
        {
            this.angle = angle;
            this.speed = speed;
        }

        public int Angle
        {
            get { return angle; }
        }

        public int Speed
        {
            get { return speed; }
        }

    }

    class RunsScoredEventArgs : EventArgs
    {
        string playerName;
        int runs;
        public RunsScoredEventArgs(string playerName, int runs)
        {
            this.playerName = playerName;
            this.runs = runs;
        }

        public string PlayerName
        {
            get { return playerName; }
        }

        public int Runs
        {
            get { return runs; }
        }
    }

 }

1) Since “BallThrownEventArgs” and “RunsScoredEventsArgs” are derived from “EventArgs”, why
did the error “No overloads for Matches…” generate
when i “Main()” is executed.

2) What do you mean by “ContraVariance” and “Covariance” (Please give me an example to understand) ?

3) The term “Contravariance” and “Covariance” are related to delegates only ?

  • 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-05-12T23:33:18+00:00Added an answer on May 12, 2026 at 11:33 pm

    The problem is that you’re trying to use variance the wrong way round.

    The idea of variance is that if your method doesn’t need as specific information as the delegate says it will provide, and if your method returns more specific information than the delegate has to return, then you’re okay to build the a delegate using that method.

    In your case, things are the other way round:

    public delegate void StatisticsDelegate(object sender, EventArgs e);
    
    static void BowlerStatistics(object sender, BallThrownEventArgs e)
    

    BowlerStatistics has to be given a BallThrownEventArgs, but StatisticsDelegate only guarantees it will provide an EventArgs.

    Suppose you could create a StatisticsDelegate from BowlerStatistics: what would you expect the following code to do?

    StaticticsDelegate del = BowlerStatistics;
    del(null, new EventArgs());
    

    BowlerStatistics wouldn’t have any ball-related information to work with.

    Or to use your existing code, but move it around a bit:

        StaticticsDelegate del = BowlerStatistics; // *
        RunsScoredEventArgs re = new RunsScoredEventArgs("Skeet", 55);
        del(DynamicDelegates.Main, re);
    
        del = BatsmanStatistics; // *
        BallThrownEventArgs be = new BallThrownEventArgs(90, 145);
        del(DynamicDelegates.Main,be);
        Console.ReadLine();
    

    This time you’re trying to make a bowler statistics delegate cope with a batsman’s statistics and vice versa! The only lines which can possibly not compile are the method group conversion ones (labelled with // * above) – so to achieve type safety, they don’t compile.

    Covariance and contravariance are tricky topics – you may find Eric Lippert’s series of blog posts useful. Prior to C# 4, variance only applies to creating delegates; as of C# 4 there’s also interface and delegate generic variance, allowing you to write:

    // Covariance
    IEnumerable<string> strings = new List<string>();
    IEnumerable<object> objects = strings;
    
    // Contravariance
    Comparer<Shape> areaSorter = (s1, s2) => s1.Area.CompareTo(s2.Area);
    Comparer<Triangle> triangleAreaSorter = areaSorter;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some code like this: class Visitor { public: Visitor(callBackFunction) {} void
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
Suppose I have this code: class Example(object): def the_example(self): itsProblem = "problem" theExample =
Suppose I have following code: public class CBase: AbstractC,IRenderable { //code here } public
Suppose I have this code (pseudocode) class SomeClass { class Person { public static
suppose you have the code template <template<class> class BaseType> class EST16 : public BaseType<int>
suppose I have this code: class D : public Base1, Base2 {} My question
Suppose I have this code: class A { }; class B: virtual public A
Suppose I have this code class Base{ public: int getVal(); private: int a, b;
Suppose we have the following code: class Test { private Test() { System.out.println(test); }

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.