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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:06:09+00:00 2026-05-10T15:06:09+00:00

I am new to all the anonymous features and need some help. I have

  • 0

I am new to all the anonymous features and need some help. I have gotten the following to work:

public void FakeSaveWithMessage(Transaction t) {     t.Message = 'I drink goats blood'; }  public delegate void FakeSave(Transaction t);  public void SampleTestFunction() {     Expect.Call(delegate { _dao.Save(t); }).Do(new FakeSave(FakeSaveWithMessage)); } 

But this is totally ugly and I would like to have the inside of the Do to be an anonymous method or even a lambda if it is possible. I tried:

Expect.Call(delegate { _dao.Save(t); }).Do(delegate(Transaction t2) { t2.Message = 'I drink goats blood'; }); 

and

Expect.Call(delegate { _dao.Save(t); }).Do(delegate { t.Message = 'I drink goats blood'; }); 

but these give me

Cannot convert anonymous method to type ‘System.Delegate’ because it is not a delegate type** compile errors.

What am I doing wrong?


Because of what Mark Ingram posted, seems like the best answer, though nobody’s explicitly said it, is to do this:

public delegate void FakeSave(Transaction t);  Expect.Call(delegate { _dao.Save(t); }).Do( new FakeSave(delegate(Transaction t2) { t.Message = expected_msg; })); 
  • 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. 2026-05-10T15:06:10+00:00Added an answer on May 10, 2026 at 3:06 pm

    That’s a well known error message. Check the link below for a more detailed discussion.

    http://staceyw1.wordpress.com/2007/12/22/they-are-anonymous-methods-not-anonymous-delegates/

    Basically you just need to put a cast in front of your anonymous delegate (your lambda expression).

    In case the link ever goes down, here is a copy of the post:

    They are Anonymous Methods, not Anonymous Delegates.
    Posted on December 22, 2007 by staceyw1

    It is not just a talking point because we want to be difficult. It helps us reason about what exactly is going on. To be clear, there is *no such thing as an anonymous delegate. They don’t exist (not yet). They are ‘Anonymous Methods’ – period. It matters in how we think of them and how we talk about them. Lets take a look at the anonymous method statement ‘delegate() {…}’. This is actually two different operations and when we think of it this way, we will never be confused again. The first thing the compiler does is create the anonymous method under the covers using the inferred delegate signature as the method signature. It is not correct to say the method is ‘unnamed’ because it does have a name and the compiler assigns it. It is just hidden from normal view. The next thing it does is create a delegate object of the required type to wrap the method. This is called delegate inference and can be the source of this confusion. For this to work, the compiler must be able to figure out (i.e. infer) what delegate type it will create. It has to be a known concrete type. Let write some code to see why.

    private void MyMethod() { } 

    Does not compile:

    1) Delegate d = delegate() { };                       // Cannot convert anonymous method to type ‘System.Delegate’ because it is not a delegate type 2) Delegate d2 = MyMethod;                         // Cannot convert method group ‘MyMethod’ to non-delegate type ‘System.Delegate’ 3) Delegate d3 = (WaitCallback)MyMethod;   // No overload for ‘MyMethod’ matches delegate ‘System.Threading.WaitCallback’ 

    Line 1 does not compile because the compiler can not infer any delegate type. It can plainly see the signature we desire, but there is no concrete delegate type the compiler can see. It could create an anonymous type of type delegate for us, but it does not work like that. Line 2 does not compile for a similar reason. Even though the compiler knows the method signature, we are not giving it a delegate type and it is not just going to pick one that would happen to work (not what side effects that could have). Line 3 does not work because we purposely mismatched the method signature with a delegate having a different signature (as WaitCallback takes and object).

    Compiles:

    4) Delegate d4 = (MethodInvoker)MyMethod;  // Works because we cast to a delegate type of the same signature. 5) Delegate d5 = (Action)delegate { };              // Works for same reason as d4. 6) Action d6 = MyMethod;                                // Delegate inference at work here. New Action delegate is created and assigned. 

    In contrast, these work. Line 1 works because we tell the compiler what delegate type to use and they match, so it works. Line 5 works for the same reason. Note we used the special form of ‘delegate’ without the parens. The compiler infers the method signature from the cast and creates the anonymous method with the same signature as the inferred delegate type. Line 6 works because the MyMethod() and Action use same signature.

    I hope this helps.

    Also see: http://msdn.microsoft.com/msdnmag/issues/04/05/C20/

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

Sidebar

Related Questions

Im very new to all coding including jquery. I though this would have been
I am new to DBMS_PROFILER. All the examples I have seen use a simple
I need to fetch all the properties of an anonymous type which can be
I have the following code: var foo = (from data in pivotedData.AsEnumerable() select new
While analyzing some ASP.NET MVC projects I got to see anonymous types scattered all
New to all this so forgive my ignorance. I am trying to figure out
im all new to framework. so the structure of code igniter looks like: system
Hello and Happy New Year all! While thinking about new project I decided to
I am very new at all this c# Windows Phone programming, so this is
I am new to all this, but here goes: There is an apple file

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.