Difference between “constant” and “readonly”Static variables in C#
Difference between “constant” and “readonly” variables in C#?
• “Const” keyword is used for making an entity constant. We cannot modify the value later in the code. Value assigning is mandatory to constant variables.It is complile time constant.
void Sum(int j)
{
const int A = 9,B = 2;
const int C = A + A;
}
If we does some changes in the code above, such as:
void Sum(int j)
{
const int A = 19, B = 22;
const int C = A + B;
}
It will give error because there is no initialization, since it's evaluated at run time.
____________________________________________________________________________
"readonly" variable value can be changed during runtime and value to readonly variables can be assigned in the constructor or at the time of declaration.It is run time constant. It means
a readonly field can be initialized either at the time of declaration or within the constructor of the same class.
Example:
_________________________________________________________________________________
"Finalize” in C#
Finalize - This method is called internally unlike Dispose method which is called explicitly. It is called by garbage collector and can’t be called from the code.This method is also used for garbage collection. So before destroying an object this method is called as part of clean up activity in C#
_____________________________________________________________________________
Static in C#
Static classes/methods/variables are accessible throughout the application without creating instance. Compiler will store the method address as an entry point.
The static keyword is used to declare a static member. we are declare a class as a static class then in this case all the class members be required to be static also. The static keyword can be used effectively with classes, fields, operators, events, methods .
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplicationstatic
{
class Program
{
public static double calArea(double Width, double height)
{
return Width * height;
}
static void Main(string[] args)
{
Double Area;
Area = Program.calArea(60, 59);
Console.WriteLine("Result is={0}",Area);
Console.Read();
}
}
}
Output:Result is=3540
• “Const” keyword is used for making an entity constant. We cannot modify the value later in the code. Value assigning is mandatory to constant variables.It is complile time constant.
void Sum(int j)
{
const int A = 9,B = 2;
const int C = A + A;
}
If we does some changes in the code above, such as:
void Sum(int j)
{
const int A = 19, B = 22;
const int C = A + B;
}
It will give error because there is no initialization, since it's evaluated at run time.
"readonly" variable value can be changed during runtime and value to readonly variables can be assigned in the constructor or at the time of declaration.It is run time constant. It means
a readonly field can be initialized either at the time of declaration or within the constructor of the same class.
Example:
class ReadOnly
{
readonly int i;
public ReadOnly( )
{
i = 100;
Console.WriteLine(i);
}
}
class ReadOnly
{
readonly int i;
public ReadOnly( ) // Contructor
{
i = 11;
Console.WriteLine(i);
}
}
_________________________________________________________________________________
"Finalize” in C#
Finalize - This method is called internally unlike Dispose method which is called explicitly. It is called by garbage collector and can’t be called from the code.This method is also used for garbage collection. So before destroying an object this method is called as part of clean up activity in C#
_____________________________________________________________________________
Static in C#
Static classes/methods/variables are accessible throughout the application without creating instance. Compiler will store the method address as an entry point.
The static keyword is used to declare a static member. we are declare a class as a static class then in this case all the class members be required to be static also. The static keyword can be used effectively with classes, fields, operators, events, methods .
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplicationstatic
{
class Program
{
public static double calArea(double Width, double height)
{
return Width * height;
}
static void Main(string[] args)
{
Double Area;
Area = Program.calArea(60, 59);
Console.WriteLine("Result is={0}",Area);
Console.Read();
}
}
}

0 Comments