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

C# Strings coding task

Hi,

Can anyone lend me a hand on structuring my Eat Method? I'd appreciate it.

Program.cs
using System;

class Program
{

  static string Eat(string fruitOne, string fruitTwo)
      return //help  


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

}

Thank you Eodell!

1 Answer

It looks like this task is trying to get you to practice a string combination method known as String interpolation. Let's break this down for just a moment.

A very basic way to combine string is to do basic concatenation:

"I think " + fruitOne + " and " + fruitTwo + " are tasty!"

With that being said, it is not very userfriendly to either type or to read (especially when the strings are much longer and include many variables). Therefore, many programming languages offer a way to interpolate (insert) the underlying values of variables into strings very easily. This process is known as string interpolation.

In C# to use string interpolation, the string must be prefixed with $. This will allow you to place C# logic (including variables) in curly braces {}. Like so:

return $"I think {fruitOne} and {fruitTwo} are tasty!";

Note that it is important that the string is prepended with $ and variables must be inbetween the curly braces ({}). using this method should make joining strings much easier moving forward.

I hope this helps:

        static string Eat(string fruitOne, string fruitTwo) {
            //return "I think " + fruitOne + " and " + fruitTwo + " are tasty!";
            return $"I think {fruitOne} and {fruitTwo} are tasty!";
        }



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