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 (Retired) Console I/O Variables

Greg Patten
Greg Patten
243 Points

Initializing a String

I am asked to Initiate bookTitle with my favorite book.

my code is:

string booktitle;

string bookTitle = "It";

Can't figure out what I'm doing wrong.

Thanks, Greg

CodeChallenge.cs
string bookTitle;

string bookTitle = "It";

1 Answer

andren
andren
28,558 Points

The type of a variable (string in this case) should only be included when you first create a variable. Not when you reference or change an existing variable.

By using it on both lines you tell C# to create the variable twice within the same method, which is invalid.

So the second line should simply be this:

string bookTitle;
bookTitle = "It"; // Removed string keyword from second line

But for this challenge you are actually expected to merge the two lines together, so you have the declaration and assignment on the same line like this:

string bookTitle = "It";

That is not something you have to do to have valid C# code, but is what this specific challenge expects your code to look like.