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# Escape Clauses and concatenation

Hi All,

I'm struggling with the task of the end of the string segment of the C# course.

I'm not actually getting an error, but when I go to check work it's pulling through a totally different set of statements, so I've clearly gone wrong somewhere.

The question states:

Define a Quote method that accepts a string parameter. It should return that same string, but surrounded by double quotes. For example, Console.WriteLine(Quote("When you learn, teach. When you get, give.")) should print "When you learn, teach. When you get, give." (Note that the output is surrounded by quotes.)

My code looks a like this:

using System;

class Program
{

    static string Quote(string myPhrase)
    {
    return (" " + (myPhrase) + " " );
    }



    static void Main(string[] args)
    {
        // Quote by Maya Angelou.
        Console.WriteLine(Quote("When you learn, teach. When you get, give."));
        // Quote by Benjamin Franklin.
        Console.WriteLine(Quote("No gains without pains."));
    }

}

If anybody could shed some light on where I've gone wrong I'd really appreciate it.

Matt

1 Answer

You are currently concatenating a space before and after the phrase. You will want to concatenate double quotes which can be done by escaping the double quote with a backslash to indicate that it should be included with the string.

using System;

class Program
{

    static string Quote(string myPhrase)
    {
    return ("\"" + (myPhrase) + "\"" );
    }



    static void Main(string[] args)
    {
        // Quote by Maya Angelou.
        Console.WriteLine(Quote("When you learn, teach. When you get, give."));
        // Quote by Benjamin Franklin.
        Console.WriteLine(Quote("No gains without pains."));
    }

}

Thank you Kris, much appreciated!