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 trialMohamed M.Kamel
Courses Plus Student 752 Pointshow to initialize a string in c#?
i want an answer
string bookTitle;
bookTitle = "name";
2 Answers
Raffael Dettling
32,999 PointsAssign the variable directly
string bookTitle ="SomeBook";
Jennifer Nordell
Treehouse TeacherHi 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!
Mohamed M.Kamel
Courses Plus Student 752 Pointsi think it can be into two lines also
Raffael Dettling
32,999 PointsAs 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
Treehouse TeacherRaffael 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.