Variables and Data Types in C#

By
A variable is an Identifier that denotes a storage location used to store a data value.Constants are unchanged during the execution of a program but a variable may take different values at different times during the execution of the program.Every variable has a type that that determines what values can be stored in the variable.
A variable name can be chosen by the programmer in a meaningful way according to the program.
Ex. 
    Length,Width,Height etc.
There are some rules to chose variable name by the programmer.
  • Variable name should not be a keyword.
  • Variable name can not start with a digits.
  • Upper case and lower case are distinct.This means that the variable Height is not the same as height or HEIGHT.
  • White space is not allowed with variable Name.
  • Variable name can be any length.
There are some properties of variables which is given below:
     1. ) Declaration of Variables:- 
A variable must be declared before it is used in the program.Variable Declaration does three things:
  • It tells compiler what the variable name is:
  • The place of declaration in the program decides the scope of the variables.
  • It specifies what type of data the variable will hold.
A variable mainly used to store a value of any data type.
The general form of declaration of a variable is:
SYNTAX:
Type  variable1,variable2,variable3 ...........variablen;
A variables are separated by commas(,) and a declaration statement must end with a semicolon.
Ex. Declaration of variable in c#:-
      int a,b;
      char c;
      float x,y;
      byte p,q;
      double money;
     2. ) Initialization of variables:-
Variables are used to store some values according to its type.After declaration of variables we need to assign some values according to its type.There are some ways to assign the value to the variables.
     2.1) Direct assignment at declaration time
        Ex. int i=0;
     2.2) Used some methods/Constructors for assignments
        Ex.

using System;
    class student
    {
        public void getdata(int x, int y)
        {
            int a = x;
            int b = y;
            Console.WriteLine("value of a is:"+a);
            Console.WriteLine("value of b is:" + b);
            Console.ReadLine();
        }

        static void Main()
        {
            Console.WriteLine("Enter two integer values");
            int p = int.Parse(Console.ReadLine());
            int q = int.Parse(Console.ReadLine());
            student st = new student();
            st.getdata(p, q);
        }
    }
   
Output:
     3. ) Default values:- 
A variable is either explicitly assigned a value or automatically assigned a default value.
There are some categories of variables that are automatically initialized to their default values.

  • Array elements
  • Static variables
  • Instance variable
The default value of a variable depends on the type of variable.some default values of variable is shown below:
     4. ) Constant variable:-

The variables whose values do not change during the execution of a program are known as Constant variables.If we want to declare any constant variables then we use 'const' keyword before the Type.
Syntax:
int k=6 ;   non constant
const int i=5 ;
const int j=i*5 ;     No error
const int m=j*k ;     Error  
Note:- we can not use non constant value with constant value in c#.
Advantage of constant:  
  • They make our programs easier to read and understand.
  • They minimize the accidental errors.
  • They make our programs easier to modify.
There are some rules when we use constants in our programs.
  • The name of constant take the same form as variable name.
  • Constants can not be declared inside a method.They declares only at class level.
  • After declaration of constants ,they can not assigned any other value.
     5. ) Scope of Variables:- 
The scope of a variable is the region of code within which the variable can be accessed.It depends on the types of the variable and place of its declaration.
There are several categories of variables in c# which is given below:
  • Array elements
  • Static variables
  • Value Parameter
  • Instance variable
  • Reference variables
  • Local variables
  • Output parameters.
Ex.

Class student
{
static int p;
int i,j;
public void display(int m,ref int n,out int q,int [] a)
{
int x=5;
char y='A';
-------------------------------------------------------
-------------------------------------------------------
}

Descriptions:- This code contains following variables which is given below:
  • p as a static
  • i,j as a instance variable
  • m as a value parameter
  • n as a reference variable
  • q as a output parameter
  • a as an array parameter
  • x,y as a local variable for display Function
Note:- 
Static and instance variables are declared at the class level.The scope of these variables  begins at the place of their declaration and ends when main method terminates.
  • Variable m,n,q and a scope will exists within the display() method.
  • Variable x,y are a local variable so scope of these variable is within blocks of curly braces { }.
Data Types in C#:-
Every variable in C# is associated with a data type.Data types specify the size and type of value that can be stored.
There are two types of Data types.
  1. Value type
  2.  Reference Type
Value type and reference type differ in two ways:
  • Where they are stored in the memory.
  • How they behave in the context of assignments statements.
     1. ) Value Type:-Value types are stored on the stack memory.When we want to copy one variable value to another variable then actual value is copied,it means two identical copy of of the value present in a stack memory.
There are two types of Value types.
  • Predefined type
  • User defined type
     1.1 ) Predefined type:- It is knows as simple types .we can categories this in follow :
  • Boolean types
  • Character types
  • Numeric types
Numeric Types further divide in three parts.
  • Floating point types
  • Integral types
  • Decimal types
Now again Integral type is divide in two parts.
  • Signed type
  • Unsigned type
     1.2 ) User defined Type:-It is also two type.
  • Structures
  • Enumerations
see it:

     1.1.1 ) Size and range of Signed ,Unsigned and Floating point types is shown in the table below:


     1.1.2 ) Decimal Type:- The decimal type is a high precision 128 bit data type that is designed for use in financial calculations.It can easily values in the range 1.0 x 10 power-28 to 7.9 x 10 power 28 with 28 significant digits.
     1.1.3 ) Character Type:-It store single character in memory.Character data type is 'char' it takes 1 byte or 8 bit.The range character is 0-255.
     1.1.4 Boolean Type:- It is used for checking the conditions in the program.
  • true
  • false
     2. ) Reference Type:- Reference type (which are of variable length) are stored on the heap.When assignment between two reference variables occurs,only reference is copied,the actual value remains in the same memory location.
There are two types of reference type.
     2.1 ) User defined type
      2.2 ) Predefined type
      2.1 ) User defined type:-User defined reference type include following things.
  • Classes
  • Delegates
  • Interface
  • Arays
     2.2 ) Predefined type:- Predefined reference type include following things:
  • Object type
  • Reference type

0 comments:

Post a Comment

Powered by Blogger.