CSharp > Step by Step - Part1

Variables in C# in CSharp

What are variables in C#?


When applications run they need to store temporary data in memory. This is done in things termed as variables. Below is a simple code which declares a variable of type integer. So this variable can only hold numeric values.

// DataType variableName

int num=10; // declares a variable with name “num” and data type integer

So let’s write a simple program which takes input from the end user , stores it in a variable and that data is displayed on the screen.

Below is the amended “program.cs” which does the same.

static void Main(string[] args)
{
       string str = Console.ReadLine().ToString(); // read data from the input keyboard
       Console.WriteLine(str); // display what is entered in the str variable
       Console.Read();// make the application wait
}

Let’s try to understand the above program step by step.

Code Explanation

// read data from the input keyboard

// indicates comments. Comments means it’s a text which the compiler will not ignore while compiling.

A properly written code is always commented so that when developer changes hand the new developer understands what logic was written.

string str = Console.ReadLine().ToString();

In this code we have declared a string variable with the name “str” and this variable get data from the keyboard due to “Console.ReadLine()” function.

Console.WriteLine(str)

This code displays the string variable data on the monitor.

Console.Read();

This statement makes the application wait so that we can see the output. Or else the application will run and quit you would not be able to see the output.

 Views: 1326 | Post Order: 10


Write for us






Hosting Recommendations