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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:30:45+00:00 2026-05-30T19:30:45+00:00

Please ignore whether a pattern like this is actually a good idea or not.

  • 0

Please ignore whether a pattern like this is actually a good idea or not. The rationale was that I wanted to catch an error that was thrown, or a mismatch in the number of records inserted / updated / deleted. I didn’t want to repeat this logic, and at the time this customisation felt like it was going to apply to no more than about four long “script” methods.

My first step was to use an anonymous function.

public void DoSqlAction(Func<bool> f, string task, string ctx, ref bool cont, List<ResultInfo> resultInfo) {
    if (cont) {
        bool ret = false;
        try {
            if (f.Invoke()) {
                resultInfo.Add(new ResultInfo(seq, task, "Success", ctx, true));
                cont = true;
            } else {
                resultInfo.Add(new ResultInfo(seq, task, "Fail", ctx, false));
                cont = false;
            }
        } catch (Exception ex) {
            resultInfo.Add(new ResultInfo(seq, task, "Error: " + ex.Message, ctx, false));
            cont = false;
        }
    }
}

if I try to use this:

 DoSqlAction(() => 1 == cx.Execute(someSql, anonymousTypeWithClassInstanceInside), "add item", refinfo.ToString(),ref cont, resultInfo);

anonymousTypeWithClassInstanceInside <– source of the error

The error comes up:

Cannot use ref or out parameter ‘abc’ inside an anonymous method, lambda expression, or query expression

The solution is to get rid of the delegate Func<bool> f. I’m writing this entry (perhaps it should be a blog post?) because it felt that the compile-time error gets generated is a bit of a road block.

In this post, I discovered a link to Eric’s article:

C# Cannot use ref or out parameter inside an anonymous method body

here

http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx

After seeing how foreach gets implemented, I was lead to think… hmmm… maybe I’m reaching for customisable syntactic sugar.

Is this possible now in C#4? Will it be possible in C#5? It makes me consider looking into http://nemerle.org at some point in the future, but I’d really like to stay within C#.

  • 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-30T19:30:47+00:00Added an answer on May 30, 2026 at 7:30 pm

    In isolation, a way better way to write this code would be:

    public ResultInfo DoSqlAction(Func<bool> f, string task, string ctx) {
        try {
            if (f.Invoke()) {
                return new ResultInfo(seq, task, "Success", ctx, true);
            } else {
                return new ResultInfo(seq, task, "Fail", ctx, false);
            }
        } catch (Exception ex) {
            return new ResultInfo(seq, task, "Error: " + ex.Message, ctx, false);
        }
    }
    

    And on the outside:

    while (/* there's stuff to do */) {
        var result = DoSqlAction(/* the next stuff */);
        infos.Add(result);
        if (!result.Succeeded)
            break;
    }
    

    or the equivalent. This eliminates the odd side-effectful function, the ref parameter, etc. And it’s shorter.

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

Sidebar

Related Questions

This is not a real situation; please ignore legal issues that you might think
I'm trying to sort an array that would look something like this: (please ignore
please ignore this post, I misread algorithm, so the question is not relevant. However,
Please ignore obvious syntax flaws in the below: I have an sql like this
Please ignore all the stuff about whether you should or not go from Sketch
(please ignore this question - it is foolish...) I want something that will do
EDIT: Please ignore this question - I've managed to solve it in a way
Hi i am new to zend and mysql so please ignore if this is
Please ignore code readability in this question. In terms of performance, should the following
I made this tiny video (please ignore if background noises) http://www.screenr.com/Qvts its 13 seconds

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.