A variable is a place to store data.
A variable has a name and a data type.
A data type determines, what values can be assigned to the variable. Integers, strings, boolean values etc.
Over the time of the program, variables can obtain various values of the same data type. Variables are always initialized to the default value of their type before any reference to the variable can be made.
using System; public class CSharpApp { static void Main() { string city = "Chennai"; string name = "Modi"; int age = 55; string nationality = "Indian"; Console.WriteLine(city); Console.WriteLine(name); Console.WriteLine(age); Console.WriteLine(nationality); city = "Delhi"; Console.WriteLine(city); } }
In the above example, we work with four variables.
string city = "Chennai";
We declare a city variable of the string type and initialize it to the "Chennai" value.
string name = "Modi"; int age = 55;
We declare and initialize two more variables. We can put two statements into one line. But for readability reasons, each statement should be on a separate line.
Console.WriteLine(city); Console.WriteLine(name); Console.WriteLine(age); Console.WriteLine(nationality);
We print the values of the variables to the terminal.
city = "Delhi";
We assign a new value to the city variable.
$ ./variables.exe Chennai Modi 55 Indian Delhi
This is the output of the example.
No comments:
Post a Comment