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 trialLandon Booth
23,395 PointsHow to Initialize in C#?
After the first string, would my code not go on to look like this? string bookTitle = "book"; System.Console.Write("Enter a book title: "); bookTitle = System.Console.ReadLine();
2 Answers
Izzatilla Karimov
9,284 PointsTry this string bookTitle = System.Console.ReadLine();
Izzatilla Karimov
9,284 Pointsand delete string bookTitle = "book"; ;)
Tom Ku
3,049 PointsIzzatilla's answer is better, as it's more efficient. The C# string are immutable objects. So, if one changes it, the compiler will create a new string object to store the new values. See the section "Immutability of String Objects" in this MSDN article (https://msdn.microsoft.com/en-us/library/ms228362.aspx).
Tom Ku
3,049 PointsTom Ku
3,049 PointsThe code you included in the questions looks fine. In the case, your code will work fine with a simple declaration, i.e. string bookTitle;