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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:02:12+00:00 2026-06-01T23:02:12+00:00

Why in code like below is the .NET compiler not able to establish that

  • 0

Why in code like below is the .NET compiler not able to establish that all code paths do return a value?

bool Test(bool param) {
    bool test = true;
    if (param)
        test = false;
    else
        test = false;
    if (!test)
        return false;
}

error CS0161: Not all code paths return a value!

The code can be refactored – but the compiler is not suggesting that. Yet all return paths are covered – so why does the compiler complain that they are not?

Edit: I guess the conclusion here is that:

(error CS0161) + (all code paths obviously return a value) => refactor code.  

Once you get the habit of that translation I guess everything is ok.

  • 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-06-01T23:02:15+00:00Added an answer on June 1, 2026 at 11:02 pm

    From the C# Language Specification 4.0 included with Visual Studio 2010.

    10.6.10 “Method body”:

    When the return type of a method is not void, each return statement in
    that method’s body must specify an expression that is implicitly
    convertible to the return type. The endpoint of the method body of a
    value-returning method must not be reachable. In other words, in a
    value-returning method, control is not permitted to flow off the end
    of the method body.

    The definition of reachability is here (emphasis added):

    8.1 “End points and reachability”:

    If a statement can possibly be reached by execution, the statement is
    said to be reachable. Conversely, if there is no possibility that a
    statement will be executed, the statement is said to be unreachable.

    …

    To determine whether a particular statement or end point is reachable,
    the compiler performs flow analysis according to the reachability
    rules defined for each statement. The flow analysis takes into account
    the values of constant expressions (§7.19) that control the behavior
    of statements, but the possible values of non-constant expressions
    are not considered
    .

    Since !test isn’t a constant expression (even though it will always evaluate to true), the compiler is obliged to not consider it in the flow analysis. One reason (maybe the only reason) for this restriction is that performing this kind of flow analysis is impossible in the general case.

    To get rid of the error, you’ll need to have another return statement, either in an else clause or unconditionally at the end of the method.

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

Sidebar

Related Questions

I've code like that below, what I want to achieve is to generate PDF
I have a code like this below, which gives me a $link that equals
my aspx (html) code is like below : this is first button (something that
Assuming that: The C# source code below is compiled under .NET 2.0 (CLR 2.0);
i call my asp.net page method like below code $.ajax({ type: POST, url: MyPage.aspx/GetItems,
my code like below public class User { public int ID { get; set;
I have a standard code like below to validate xml against xsd, but it
I am using SQL Developer to debug code like below, the output result will
This has been bothering me lately- when I use some code like below to
My code looks like below: private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e) { // TODO:

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.