read variable in csharp

To read a variable in C#, you first need to declare the variable with a data type. This tells the program what type of value the variable can hold. Then, you can use the Console.ReadLine() method to read input from the user and assign it to the variable.

Here's an example:

main.cs
// Declare a variable of type string
string name;

// Ask for the user's name and read input from the console
Console.WriteLine("What is your name?");
name = Console.ReadLine();

// Display the user's name back to them
Console.WriteLine("Hello, " + name + "!");
262 chars
10 lines

In this example, the name variable is declared as a string. The Console.WriteLine() method is used to prompt the user to enter their name, and the Console.ReadLine() method reads the input from the console and assigns it to the name variable. Finally, the program displays a message that includes the user's name using string concatenation.

gistlibby LogSnag