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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:08:32+00:00 2026-05-29T09:08:32+00:00

I am working on a .NET project where the team decided to sidestep the

  • 0

I am working on a .NET project where the team decided to sidestep the convention of Exceptions in favor of a custom error signal. In practice, the concept works well enough, except there is one nasty side effect: I find myself constantly checking intermediate error codes before deciding whether to proceed:

var error = DoSomething();

if( !error.Success )
    return error;

return DoSomethingElse();

What I would like to do is something like the following:

DoSomething().ReturnIfError();
return DoSomethingElse();

I know some languages such as Ruby support the ‘return if’ construct – is there any such thing in C#, or would it be possible to do so in an extension method?

  • 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-29T09:08:33+00:00Added an answer on May 29, 2026 at 9:08 am

    This is not something that can be prettily hacked in with extension methods.

    In the second example you gave (with the theoretical method chaining), you would…

    • call DoSomething
    • if DoSomething errored out, call ReturnIfError
    • then call DoSomethingElse

    You need to use the return keyword to break out of a method block early.

    Assuming that this is what you really want to do, then the following code can make that happen (not that I recommend it!).

    using System;
    
    namespace ConsoleApplication6
    {
        public static class ClassUtility
        {
            public static void CallOnError<TIn>(this TIn value, Func<TIn, bool> fn, Action<TIn> errorFn)
                where TIn : class
            {
                if (!fn(value))
                {
                    errorFn(value);
                }
            }
        }
    
        public class TestClass
        {
            public bool FirstCall(bool value)
            {
                Console.WriteLine("First call.");
                return value;
            }
    
            public void SecondCall()
            {
                Console.WriteLine("Second call.");
            }
    
            public void ThirdCall()
            {
                Console.WriteLine("Third call.");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                var testClass = new TestClass();
    
                testClass.CallOnError(tc => tc.FirstCall(false),
                                      tc2 => tc2.SecondCall());
    
                testClass.CallOnError(tc => tc.FirstCall(true),
                                      tc2 => tc2.SecondCall());
    
                testClass.ThirdCall();
    
                Console.ReadLine();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently joined a team that is working on a ASP.NET MVC project.. they
I am working on a ASP.Net project with a team of 3 members. Due
My team is working with a .NET 2008 DB Project in their solution so
My team and I are working on a .net web app project for the
My team is currently working on windows forms project using VB.NET. We are curently
My team working in a project using asp.net mvc3(c#). Based on the project requirement,we
Currently our team of 11 people is working on a project on asp.net platform.
everyone. I am currently working in a .net mvc3 project team. Today ,my team
I am working on an asp.net MVC 3 project as a team member. This
I am currently working on an ASP.NET MVC project. Some developers on the team

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.