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

Mohamed M.Kamel
PLUS
Mohamed M.Kamel
Courses Plus Student 752 Points

how to initialize a string in c#?

i want an answer

CodeChallenge.cs
string bookTitle;
bookTitle = "name";

2 Answers

Raffael Dettling
Raffael Dettling
32,999 Points

Assign the variable directly

string bookTitle ="SomeBook";
Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! There's not really anything wrong with your code other than a misinterpretation of what they mean. They want you to declare the variable and initialize it. This means that the declaration and the initial value should happen on the same line.

So, if I alter your code just a bit, it passes:

string bookTitle = "name";

This declares a string named bookTitle and initializes it by giving it a beginning value.

Hope this helps! :sparkles:

Raffael Dettling
Raffael Dettling
32,999 Points

As Jennifer says itΒ΄s not wrong to declare variable and assign a value to it afterwards. But to pass this challenge you have to declare and assign the variable immediately :)

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Raffael is correct. Your code ultimately has the same effect, but it's not what they've asked you to do exactly. These challenges are very strict and you must follow the instructions to the letter. "Initializing" a variable means that you declare a variable and give it an initial (or starting) value at the time of the declaration.