30 Jan C# Variables
In this lesson, learn how to work with C# Variables. Variables in Java are reserved areas allocated in memory. It is a container that holds value. Each variable is assigned a type, which is known as data type.
The reserved area set for the variable marks is shown below. The variable is assigned a value of 95. Also, you can see, we need a data type to declare a variable. The int data type is used below:
Declare a variable
To declare a variable in C#, mention the datatype followed by the variables i.e.
1 2 3 |
dataType variables; |
The above syntax displays that it is so easy to declare variables, for example,
1 2 3 4 |
int marks; float salary; |
Initialize Variables in C#
To initialize a variable in C#, assign a value to it. Here are some examples:
1 2 3 4 |
int marks = 95; float salary = 3000.50; |
Variables – Example
Let us now see a complete example for C# variables and print them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using System; namespace Demo { class Studyopedia { static void Main(string[] args) { Console.WriteLine("Variables in C#...\n"); int i, j, k, prodRes; i = 2; j = 3; k = 5; Console.WriteLine("First Number = {0}", i); Console.WriteLine("Second Number = {0}", j); Console.WriteLine("Third Number = {0}", k); prodRes = i*j*k; Console.WriteLine("Multiplication Result = {0}", prodRes); Console.ReadLine(); } } } |
Output
1 2 3 4 5 6 7 8 |
Variables in C#... First Number = 2 Second Number = 3 Third Number = 5 Multiplication Result = 30 |
No Comments