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 791847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:54:08+00:00 2026-05-14T21:54:08+00:00

public class Program { delegate void Srini(string param); static void Main(string[] args) { Srini

  • 0
public class Program
{
    delegate void Srini(string param);

    static void Main(string[] args)
    {
        Srini sr = new Srini(PrintHello1);
        sr += new Srini(PrintHello2);      //case 2:      
        sr += new Srini(delegate(string o) { Console.WriteLine(o); });
        sr += new Srini(delegate(object o) { Console.WriteLine(o.ToString()); }); //case 4:
        sr += new Srini(delegate { Console.WriteLine(“This line is accepted,though the method signature is not Comp”); });//case 5
        sr("Hello World");
        Console.Read();
    }       

    static void PrintHello1(string  param)
    {
        Console.WriteLine(param);
    }

    static void PrintHello2(object param)
    {
        Console.WriteLine(param);
    }
}

Compiler doesn’t complain about the case 2 (see the comment), well, the reason is straight forward since string inherits from object. Along the same lines, why is it complaining for anonymous method types (see the comment //case 4:) that

Cannot convert anonymous method to delegate type ‘DelegateTest.Program.Srini’ because the parameter types do not match the delegate parameter types

where as in case of normal method it doesn’t? Or am I comparing apples with oranges?
Another case is why is it accepting anonymous method without parameters?

  • 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-14T21:54:09+00:00Added an answer on May 14, 2026 at 9:54 pm

    Method group conversions support variance (as of C# 2 – they didn’t in C# 1), anonymous function conversions simply don’t. Given that whenever you write an anonymous function you can write the appropriate parameter, why not just do so? There would be no benefit in allowing variance there as far as I can see, and it would make the rules harder to get right. (Variance ends up being pretty complex in the spec.)

    EDIT: An anonymous method without a parameter list is basically compatible with any delegate’s parameter list so long as it doesn’t have out parameters. Basically it’s a way of saying, “I don’t care about the parameters.” It’s the one feature of anonymous methods that lambda expressions don’t have 🙂

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

Sidebar

Related Questions

Suppose public class Program { public static void Main(string[] args) { Program p =
I am compiling a simple program class Test { public static void main(String[] args)
class Program { public delegate void VoidMethodDelegate(); public delegate int IntMethodDelegate(); static void Main(string[]
using System; public delegate void Printer(string s); class Program { public static void Main(string[]
If I run the following program: class Runit{ public static void main(String[] argsWut) throws
In a typical winform program.cs contains the entry point: static void Main(string[] args) {
Consider the following program: class A { public static void Foo() { } }
I have a delegate function class FuncActivator { public delegate void DelFunc(string arg, string
class Program { public class Pokemon { public string Name; public int Weight; }
I have the following code: public class Foo {} static class Program { [XmlElement(foo)]

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.