Constant
Constant is variable of a class of which the value remain fixed.They are declared without using static keyword. we can use a constant in the class in which it is defined, referring to it by name. we can also use the constant in other classes, prefixing the name of the constant with the class name. it's mandatory to assign a value to it. By default a constant is static and we cannot change the value of a constant variable throughout the entire program. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.Defining Constants
Constants are defined using the const keyword. Syntax for defining a constant is:const <data_type> <constant_name> = value;
Ex.
const double Pi = 3.14159;
const double Rvalue = 1.8766;
const string Name = "DotNet Bird";
We cannot include the static keyword when declaring a constant. A constant by default static, so the static keyword would be will generate an error if you use before it.
/ This is Correct
const string Remarks = "Exellent";
// This is incorrect
static const int Age = 42;
Defining Contant inside Class : We can define a constant within a class that contains related data and methods or a class, containing a collection of constants.
public class contants
{
Public const double Pi = 3.14159;
Public const double Rvalue = 1.8766;
Public const string Name = "DotNet Bird";
}
Initialization of a Constant
`A constant must be initialized at time of decalaration . Otherwise, it will give an error like "A const field requires a value to be provided
// This is Correct
const string Remarks = "Exellent";
// This is incorrect
const string Remarks;
Using Constant in Program: Let us use a constant in a program to clarify a constant
using System;
namespace DeclaringConstants
{
class Program
{
static void Main(string[] args)
{
const double pi = 3.14159;
// constant declaration
double r;
Console.WriteLine("Enter Radius of Circle: ");
r = Convert.ToDouble(Console.ReadLine());
double Circle_Circumfrence = 2 * pi * r;
Console.WriteLine("Radius: {0}, Circumfrence: {1}",r, Circle_Circumfrence);
Console.ReadLine();
}
}
}
Output:
Enter Radius:
3
Radius: 3, Circumfrence: 18.84954
Point to Remember for Constant:
1. Const can only be initialized at the time of declaration of the field.2. Const values will evaluate at compile time only means Compiler evaluates the const variables.
3. Const value can’t be changed these will be same at all the time.
4. You cannot reassign a const variable.
5. The static modifier is not allowed in a constant declaration. By default constant are static, hence you cannot define a constant type as static.
6. A const field of a reference type other than string can only be initialized with null.
7. You can apply const keyword to built-in value types (byte, short, int, long, char, float, double, decimal, bool), enum, a string literal, or a reference type which can be assigned with a value null
8. Constants can be marked as public, private, protected, internal, or protected internal access modifiers. Use the const modifier when you sure that the value of a field or local variable would not be changed.
ReadOnly
A readonly field can be initialized either at the time of declaration or with in the constructor of same class. Therefore, readonly fields can be used for run-time constants.
Read only values will evaluate at runtime only.
Explicitly, you can specify a readonly field as static since, like constant by default it is not static. Readonly keyword can be apply to value type and reference type (which initialized by using the new keyword) both. Also, delegate and event could not be readonly.
Use the readonly modifier when you want to make a field constant at run time.
Example
public class Readonly{
public const int i = 2;
public readonly int x;
public Readonly()
{
x = 3;
}
}
Note: if you donot want to change the value of the constant , use a const or if you have a constant that may change o, use a readonly.
Static ReadOnly:
A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime
Static
The static keyword is used to specify a static member and common to all the objects .If static member is changed by one object, it is expressed by other object of the class means they do not tied to a specific object. static keyword can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.class MyClass
{
static int age = 20;
int i = 5;
public static void Show()
{
Console.WriteLine(age);
Console.WriteLine(i); //error, since you can access only static members
}
}
Note:
1. If the static keyword is applied to a class, all the members of the class must be static.
2. Static methods can only access static members of same class. Static properties are used to get or set the value of static fields of a class.
3. Static constructor can't be parameterized. Access modifiers can not be applied on Static constructor, it is always a public default constructor which is used to initialize static fields of the class.
Difference between const and readonly
- const fields has to be initialized while declaration only, while readonly fields can be initialized at declaration or in the constructor.
- const variables can declared in methods ,while readonly fields cannot be declared in methods.
- const fields cannot be used with static modifier, while readonly fields can be used with static modifier.
- A const field is a compile-time constant, the readonly field can be used for run time constants.
Static Readonly Fields vs. Constants
A static readonly field in a class is very similar to a constant. Both are a constant and static value with a little differenceAs a static readonly field in a Tree class:
1
|
public static
readonly string
treeCatagory = "Tree with fruits";
|
public const
string treeCatagory = " Tree with fruits ";
|
The value of a static readonly field should be available at run-time. It is specified in either the declaration or within a static constructor.
Use a readonly field for values that you won’t know until run-time or in doubt. Use a constant for values that are known and never change.
x
Comments
Post a Comment