Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,998 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,998 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.