codescracker


c#

C# Variables



« Previous Tutorial Next Tutorial »


Variable is simply a name given to a storage area that your C# program can manipulate with. Each variable in C# has a specific data type, determines the size and the layout of the variable's memory and the range of values that can be stored within that memory and the set of operations that can be applied to that variable. Here are the basic types in C#:

Variable Definition in C#

Here is the general form to define variable in C#:

data_type comma_separated_variable_list;

Here data_type refers to any valid C# data type like char, int, float, etc. And comma_separated_variable_list refers to the variable. The variable can be one or more than one. Here are some valid variable definition in C#:

int l, m, n;
char c1, c2;
float f1, f2, total;

Variable Initialization in C#

Here is the general form to initialize a variable in C#:

variable_name = value;

Here is the general form to define and initialize a variable in C#:

int l=10;

Here are some valid variable definition, and variable definition and initialization in C#:

int l, m=20, n;
l=30;
n=50;

C# Variable Example

Here is an example program, illustrating variables in C# programming:

/* C# Variables - Example Program */
		
using System;
namespace VariableExample 
{
   class Variableprogram 
   {
      static void Main(string[] args) 
      {
         short snum;
         int inum;
         double dnum;
         snum = 100;
         inum = 200;
         dnum = snum + inum;
         Console.WriteLine("snum = {0}, inum = {1}, dnum = {2}", snum, inum, dnum);
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it would produces the following output:

snum = 10, inum = 20, dnum = 300

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test