C# C# Basics (Retired) Console I/O Variables

how do i declare a string variable for example bookTitle

am a little lost i need to declare a string variable

CodeChallenge.cs
String bookTitle = System.Console.ReadLine ();

3 Answers

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
20,983 Points

Giving a message to the screen

System.Console.Write("Enter a book title: ");

Taking the input from the user, putting it in variable called bookTitle

string bookTitle = System.Console.ReadLine();
Liam Clarke
MOD
Liam Clarke
Treehouse Moderator 19,187 Points

Hi,

To declare a string you just give the variable type 'string' and the name of it.

string bookTitle;

You then just assign the variable a value.

string bookTitle = "Harry Potter";

Note

Your actually not entirely wrong using uppercase 'S' but there are some very slight differences between "String" and "string".

this small article does a good job at explaining it

Good luck!

okay thank you and what if i want to Use the System.Console.Write() method to print "Enter a book title: " to the console.

Liam Clarke
MOD
Liam Clarke
Treehouse Moderator 19,187 Points

System.Console.Write(); is a method that in your case takes a String as a parameter.

System.Console.Write("Enter a book title: ");

System.Console.Readline() is another method that takes the next line of characters from an input so it does not need any parameters.

System.Console.Write("Enter a book title: ");
string bookTitle = System.Console.ReadLine();