A variable is a name given to a storage area that is used to store values of various data types. Each variable in C# needs to have a specific type, which determines the size and layout of the variable’s memory.
For example, a variable can be of the type String, which means that it will be used to store a string value. Based on the data type, specific operations can be carried out on the variable.
Variables
In the following example, message is the name of the variable that stores the string data value “Hello World!!“. As the name suggests, the contents of a variable can vary, i.e., you can change the value of a variable at any time.
namespace CSharpTutorials { class Program { static void Main(string[] args) { string message = "Hello World!!"; Console.WriteLine(message); Console.ReadLine(); } } }
Variables are containers for storing data values.
In C#, there are different types of variables (defined with different keywords), for example:
int
– stores integers (whole numbers), without decimals, such as 123 or -123double
– stores floating point numbers, with decimals, such as 19.99 or -19.99char
– stores single characters, such as ‘a’ or ‘B’. Char values are surrounded by single quotesstring
– stores text, such as “Hello World”. String values are surrounded by double quotesbool
– stores values with two states: true or false
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:
Syntax
type variableName = value;
Examples
Create a variable called name of type string and assign it the value “John”:
string name = "John"; Console.WriteLine(name);
Create a variable that should store a number, called myNum of type int and assign it the value 15:
int myNum = 15; Console.WriteLine(myNum);
You can also declare a variable without assigning the value, and assign the value later:
int myNum; myNum = 15; Console.WriteLine(myNum);
Note that if you assign a new value to an existing variable, it will overwrite the previous value.
Change the value of myNum to 20:
int myNum = 15; myNum = 20; // myNum is now 20 Console.WriteLine(myNum);
Multiple Declarations
int i, j, k, l = 0; int amount, num;
Multi-Line Declarations
int i, j, k, l = 0;
Variable Assignment
int i = 100; int j = i; // value of j will be 100
Constants
You can add the const keyword before the type of your variable if you don’t want others (or yourself) to overwrite existing values (this will declare the variable as “constant”, which means unchangeable and read-only). The const keyword is useful when you want a variable to always store the same value.
const int myNum = 15; myNum = 20; // error
Syntax for other types of variables
int myNum = 5; double myDoubleNum = 5.99D; char myLetter = 'D'; bool myBool = true; string myText = "Hello";
Display variables
The WriteLine()
method is often used to display variable values to the console window.
string name = "John"; Console.WriteLine("Hello " + name);
You can also use the + character to add a variable to another variable:
string firstName = "John "; string lastName = "Doe"; string fullName = firstName + lastName; Console.WriteLine(fullName);
For numeric values, the + character works as a mathematical operator (notice that we use int (integer) variables here):
int x = 5; int y = 6; Console.WriteLine(x + y); // Print the value of x + y
Sources :
https://www.tutorialsteacher.com/csharp/csharp-tutorials
https://www.w3schools.com/cs/default.asp