About
In this code snippet, we will learn how to use variables in C#.
A variable is a “container” that holds a value. The value can be any of the many data types in C#. To make a variable you must first declare its data type followed by the name you will use to reference that particular variable. Then you can assign it a value using the = assignment operator.
Let’s see how to declare and use variables in the code example below.
Code:
using System; namespace Variables { class Program { static void Main(string[] args) { //To declare a variable you first define the data type and then the name of the variable; int myVariable; //Use the = assignment operator to assign a value to your variable. myVariable = 10; //Declaration and assignment all at once. int myOtherVariable = 1; Console.WriteLine(myVariable); Console.WriteLine(myOtherVariable); Console.ReadLine(); } } }