Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Define an Eat method invincible Console.WriteLine(); input?

Hello, I have an issue with Define Eat method string challenge.

static string Eat(string first, string second) { Console.WriteLine($"I think {first} and {second} are tasty!"); return Console.ReadLine(); }

static void Main(string[] args) { Console.WriteLine(Eat("apples", "blueberries")); Console.WriteLine(Eat("carrots", "daikon")); }

However it prints out the following words: I think eggs and falafel are tasty!

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Test.Main(String[] args) in /workdir/Test.cs:line 20

Is there a bug in the system or have I written the code wrongly? I gave it a testrun with the exact same code on VSCODE and it prints out

I think apples and blueberries are tasty!

1 Answer

Steven Parker
Steven Parker
231,271 Points

The instructions say that "Eat should return a string ...", but this code is writing the string to the console instead.

Also, the instructions don't ask for anything to be read from the console.

So I should not add a console?

Like this for example:

static string Eat(string first, string second)
{
    return $"I think {first} and {second} are tasty";
}


static void Main(string[] args)
{
   Console.WriteLine(Eat("apples", "blueberries"));
   Console.WriteLine(Eat("carrots", "daikon"));     
}
Steven Parker
Steven Parker
231,271 Points

Very close! Now you're only missing the exclamation point (!) at the end of the string.

Thank you! I was frustrated because it kept sending me to eggs and falafel which I had no clue over