How to initialize variables in C#?


A variable is a name given to a storage area that our programs can manipulate.

Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.

To initialize a variable, first you need to define it.

int a;

Now to initialize, use an equal sign followed by a constant expression.

int a = 10;

We used the following syntax above.

variable_name = value;

Let us see a simple example to work with variable in C#.

Example

 Live Demo

using System;
namespace VariableDefinition {
   class Program {
      static void Main(string[] args) {
         short a;
         int b ;
         double c;
         a = 10;
         b = 20;
         c = a + b;
         Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
         Console.ReadLine();
      }
   }
}

Output

a = 10, b = 20, c = 30

Updated on: 23-Jun-2020

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements