find and solve ||
Please wait.....

C# Variables

A C# variable is a block of memory where data or value of a particular data type is tored.Every vaiable has a name,data type,and and initial value. The name or identifier of the variable allows you to access the memory location where its value is stored. The data type of the variable informs the compiler about the size fo memory that the variable occupies.  IN addition, the data type indicates the type of values the variable can store. The value that you store in a vaiable can be changed or modified any number of times in the program.

Note that the name of a variable has to be unique in a given scope.When you give a name to a variable,you need to remember the following points:

  •  The variable name can contain only letters,digits,and the underscore character (_).
  •  The variable name cannot start with a digit.
  •  The variable name in uppercase and lowercase is distinct;for example,salary and SALARY are tow different variables.This is because C#   is a case-sensitive language.
  •  The variable name cannot be a keyword
  •  The variable name cannot contain white spaces.

 The following is a list of some valid variable name in C#:

  •  salary
  •  totalAmount,
  •  yearlyCost,
The following is a list of some invalid variable names in C#:

  • total#amount
  • double
  • 9number
In the preceding list,total#amount is not a valid variable name because it contains the hash character(#),which cannot be used in a variable name.double is also an invalid variable name as it is a serer ed keyword that cannot be used as a variable name. Similarly, 9number cannot be used as a variable name as it start with a digit.

Besides variables,C# also allows you to create constants,which are placeholders for values that do not change during the execution of the program.Once you have assigned a value to a constant,you cannot modify the value of constant later in the program.Therefore,you can use constants to store static values,for instance value of Pi.

In this section,you now learn to perform the following tasks:

Declare variables
Initialize variables

Let's Learn to perform each of these tasks,one by one.

Declaring Variables

In C# you need to declare a variable before using it in a program.Declaring a variable is necessary because by declaring the variable in program,you create the variable.As a result, the compiler is aware of the variable and identifies it wherever it is used in the program.While declaring a variable,you need to specify the name and data type of the variable.

The syntax to declare a variable is given as follows:

<Data_Type>  <Variable_Name>
int the preceding syntax,

 Data_Type :Refers to the data type of the variable to be created.
 Variable_Name : Refers to the name of the variable declaration in C#.

The following code snippet shows examples of variable declaration in C#:

string  firstName;
int age;
int weight;

Initializing Variables

In C#, you cannot use a variable unless you assign a value to it.Variable assignment refers to the process of providing values to a variable.When you assign the first or initial value to a variable,it is referred to as initializing the variable.After initializing a variable,you can use the variable in the program as many times as you want you can also change or replace the initial value and assign a new value to the variable.

 The syntax to assign values to a variable is as follows:


<Variable_Name>:Refers to the name of the variable
= :Refers to the assignment operator
Value : Refers to the value that is to be stored int the variable

The following code snippet shows how to assign values to variables:


using System;
namespace VariableExample {
  public class Program {
      public static void Main(string[] args) {
         short a;
         int b ;
         double c;

         /* actual initialization */
         a = 10;
         b = 22;
         c = a + b;
         Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);

a = 10, b = 22, c = 32

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

If felt valuable to you, feel free to share it.


Report Response