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

  • Home
  • SEARCH
  • 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 7606029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:18:33+00:00 2026-05-31T00:18:33+00:00

Scenario: Let’s say for example, I have two functions. First function is concerned with

  • 0

Scenario:

Let’s say for example, I have two functions.
First function is concerned with reading a single bit, thus returing true or false.
Second function is concerned with reading a variable number of bits using first functions n times where n is the number if bits.

First Function:

private bool ReadBit ( )
{
    .
    .
    .
}

Second Function: ( Recursive is using here instead of loop iteration well-known techniques )

public List<bool> ReadBits ( int Value ) //Value = Number of Bits
{
    List<bool> Result = new List<bool> ( );

    if ( Value == 0 )
    {
        return Result;
    }
    else
    {
        Result . Add ( ReadBit ( ) );

        return ReadBits ( --Value );   //OPTION 1
        ReadBits ( --Value );   //OPTION 2

    }
}

I Know OPTION 2 will throw an error about “not all code paths return a value“.
This is not the problem as I can deceive the compiler many ways !

My Question:

What are the real deference between OPTION 1 & OPTION 2 ?
I swear both will do the recursive concept if we add a return line somewhere with OPTION 2 & re-order lines somewhat.

  • 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-31T00:18:34+00:00Added an answer on May 31, 2026 at 12:18 am

    The difference is this:

    • In option 1 you return the wrong result.
    • In option 2 you return no result (and this is invalid, hence the compiler error).

    Of course, you want neither. You want to return the correct result:

    Result.Add(ReadBit());
    Result.AddRange(ReadBits(value - 1));
    return Result;
    

    That is, you first add the currently read bit, and then append all the next read bits (that were created recursively).

    However, this is wildly inefficient. It’s much more efficient to reverse the process, thus creating only a single list.

    public List<bool> ReadBits(int Value)
    {
        if (Value == 0)
        {
            List<bool> Result = new List<bool>();
        }
        else
        {
            List<bool> Result = ReadBits(value - 1);
            Result.Add(ReadBit());
        }
        return Result;
    }
    

    But note that this will of course reverse the order of your result list.

    Much as I like recursion, why not go with the iterative approach here that can be expressed much more naturally in C#?

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

Sidebar

Related Questions

Scenario: Let's say I have two tables, TableA and TableB. TableB's primary key is
I have a scenario which I'm a bit stuck on. Let's say I have
Scenario: Let's say I have four very similar applications (i.e. most of the functionality
Let's say I have a scenario where I have a global plugin (or at
To simplify the scenario, let's say we have a list of People with FirstName
Let's say I have the following scenario: ComObjectClass firstCOMObject = new ComObjectClass(); ComObjectClass secondCOMObject
Let's me show first the scenario that is working: I have a HandlerAttribute that
I have the classic scenario with 2 tables and a junction table. Let's say,
Let me explain my scenario first: I have around 2000 tests to run, which
Let's say I have a scenario where I have a Service that is responsible

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.