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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:21:17+00:00 2026-05-28T02:21:17+00:00

I am sure I am making a foolish mistake here but it beats me.

  • 0

I am sure I am making a foolish mistake here but it beats me. Here is a piece of code:

void Main()
{
    //Key is a parent while the list contains its children
    Dictionary<string,List<string>> d = new Dictionary<string,List<string>>();
    d.Add("1",new List<string>(){"2","3"});//valid.should return false 
    d.Add("2",new List<string>(){"4"});//valid.should return false
    d.Add("3",new List<string>(){"5"});//valid.should return false
    d.Add("4",new List<string>(){"1"});//invalid.should return true 

   IsChildAlreadyAParent("4","2",d);
 }

private bool IsChildAlreadyAParent( string child, string parent, Dictionary<string, List<string>> d )
{           
    if( !d.ContainsKey( child ) || ( d.ContainsKey( child ) && d[child].Count == 0 ))
    {
        return false;
    }

    foreach( string childOfChild in d[child] )
     {
        if( childOfChild == parent )
            return true;

        if( IsChildAlreadyAParent( childOfChild, parent, d ) ) return true;
    }
}

Compiling this gives me this error:

IsChildAlreadyAParent(string, string, System.Collections.Generic.Dictionary<string,System.Collections.Generic.List<string>>)': not all code paths return a value

I have read the code a few times and I cant see how a return condition would be missed. I know I can rectify it by adding a method return statement before the method end but it doesn’t help me understand the issue at hand. Where is the gap??

  • 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-28T02:21:17+00:00Added an answer on May 28, 2026 at 2:21 am

    You might think this executes the loop body exactly once, always returning from the function:

    foreach( string childOfChild in d[child] )
    {
        if( childOfChild == parent )
            return true;
    
        return IsChildAlreadyAParent( childOfChild, parent, d );
    }
    

    But what if d[child] has no elements at all?

    Also, testing only the first child probably isn’t the right solution either.

    Better:

    foreach( string childOfChild in d[child] )
    {
        if( childOfChild == parent ) return true;
        if (IsChildAlreadyAParent( childOfChild, parent, d )) return true;
    }
    return false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, I'm sure making a silly mistake, but did not work out. I'm compiling
Stupid question, but just making sure here: When should I use TCP over HTTP?
I am sure making a silly mistake but I can't figure what: In SQL
The following is some code for making sure people can't submit if the value
I'm going through some old C#.NET code in an ASP.NET application making sure that
I'm going through some old C#.NET code in an ASP.NET application making sure that
How to generate random integers but making sure that they don't ever repeat? For
I have this code that makes sure your are logged in, and then making
I want to display a float as a string while making sure to display
I'm trying to validate a name making sure the name wasn't use but for

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.