Codecademy Logo

Learn C#: Data Types and Variables

Variables and Types

A variable is a way to store data in the computer’s memory to be used later in the program. C# is a type-safe language, meaning that when variables are declared it is necessary to define their data type.

Declaring the types of variables allows the compiler to stop the program from being run when variables are used incorrectly, i.e, an int being used when a string is needed or vice versa.

string foo = "Hello";
string bar = "How are you?";
int x = 5;
Console.WriteLine(foo);
// Prints: Hello

Learn More on Codecademy