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#

How 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();

Tom Ku
Tom Ku
3,049 Points

The code you included in the questions looks fine. In the case, your code will work fine with a simple declaration, i.e. string bookTitle;

2 Answers

Izzatilla Karimov
Izzatilla Karimov
9,284 Points

Try this string bookTitle = System.Console.ReadLine();

Izzatilla Karimov
Izzatilla Karimov
9,284 Points

and delete string bookTitle = "book"; ;)

Tom Ku
Tom Ku
3,049 Points

Izzatilla'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).