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# C# Basics Strings Combining Strings

Issue with string interpolation method

//This code works in a repl on another website

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

Program.cs
using System;

class Program
{

static string Eat(string foodOne, string foodTwo) {
  Console.WriteLine($"I think " + foodOne + " and " + foodTwo + " are tasty!");

    return Console.ReadLine();

}


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

}

2 Answers

Antonio De Rose
Antonio De Rose
20,885 Points

this is defeating the whole purpose of the interpolation, you can have one whole string within only a pair of double quotes, the other error is string interpolation, should not be mixed with string concatenation, you are using + operator for string interpolation, with string interpolation, the variables will be within the curly braces.

concatenation could be used with 2 ways

1) STRING CONCATENATION

2) STRING INTERPOLATION

do not mix it

you have 3 pairs of double quotes.

you should not use console readline too, as you have nothing to enter in your console, if there is a quetion, and then if you have anything to enter, then you use readline, this case no.

you have to return only, and when you return you should not use within braces.

your approach is right.

static string Eat(string foodOne, string foodTwo) {
  //Console.WriteLine($"I think " + foodOne + " and " + foodTwo + " are tasty!"); // this is wrong

    //return Console.ReadLine();  / this is wrong

//let us rephrase

return $"I think {foodOne} and {foodTwo} are tasty!";

}

Hi there. I've been struggling with this challenge all weekend. Could you tell me why we need curly braces around foodOne and foodTwo? We didn't need them in the earlier lessons when we were dealing with parameters and variables. Or am I remembering that wrong?

Also, I'm confused about the double quotes, they're only used to create strings right? Why do we need these double quotes when we already set the type to string in the Eat method?

Thank you, I appreciate your help.