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

Vygandas Razhas
PLUS
Vygandas Razhas
Courses Plus Student 2,422 Points

Initializing a string variable in C#? I'm so lost.

Someone please verse me with the answer? Thank you!

CodeChallenge.cs
string bookTitle;

2 Answers

andren
andren
28,558 Points

Initializing is basically just a complex way of stating that you should assign it a value. Assigning values to variables is done by using the = (equal) operator. Like this:

string bookTitle = "Book title";

That code will assign the string "Book title" to the variable bookTitle.

Umut İnanç
PLUS
Umut İnanç
Courses Plus Student 818 Points

Initializing just means you are creating a variable and assign it a value for the first time.

In this example, you initialize a variable with a value of "Book Title":

string bookTitle = "Book Title";

However, if you create a variable for the first time and don't assign it any value yet, it's called declaring a variable. Like in this example:

string bookTitle;

You can always assign it a value later using the = operator like in this example:

string bookTitle;


bookTitle = "Book Title";