Variables are essentially "containers" for storing data values. Whenever you need your program to remember a piece of information, you store it in a variable.
C# is a strongly-typed language. This means you must explicitly declare the type of data a variable will hold when you create it.
To create a variable, you specify the type and assign it a value:
type variableName = value;
int - Stores integers (whole numbers), without decimals, such as 123 or -123.double - Stores floating point numbers, with decimals, such as 19.99 or -19.99.char - Stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes.string - Stores text, such as "Hello World". String values are surrounded by double quotes.bool - Stores values with two states: true or false.Let's create one variable of each common type:
using System;class Program { static void Main() { int myNum = 50; double myDoubleNum = 5.99; char myLetter = 'D'; bool myBool = true; string myText = "Hello"; Console.WriteLine(myNum); Console.WriteLine(myDoubleNum); Console.WriteLine(myLetter); Console.WriteLine(myBool); Console.WriteLine(myText); } }
const)If you want a variable to store a value that should never change throughout your program, you can use the const keyword. This creates a constant variable. If you try to change it later, C# will throw an error.
const int myNum = 15; // myNum = 20; <-- This would cause an error
Which data type is used to store text like "Hello"?