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:

Csharp Variables

Declare a variable

To declare a variable in C#, mention the datatype followed by the variables i.e.

dataType variables;

The above syntax displays that it is so easy to declare variables, for example,

int marks;
float salary;

Initialize Variables in C#

To initialize a variable in C#, assign a value to it. Here are some examples:

int marks = 95;
float salary = 3000.50;

Variables – Example

Let us now see a complete example for C# variables and print them:

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

Variables in C#...

First Number = 2
Second Number = 3
Third Number = 5
Multiplication Result = 30
C# Type Casting
C# Introduction
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment