This article will explain some important points about the const, readonly and static keywords in C#. We often see most developers unable to understand the differences among the const, readonly and static keywords in C#. I have recently also tried looking at tutorials (articles) around the web and I have seen some examples about these keywords. But I can't find a good solution to this. Such as why and when. This article answers the question that we often see around the web, of why and when to use the const, readonly and static keywords.
This is a very simple article but will clarify some of the concepts of C# for those who are learning or for those also who might have a good knowledge but unfortunately miss these important keywords.
The following points defines the const, readonly and static keyword in C#.
const Keyword
- const fields or local variables must be assigned a value at the time of declaration.
- They cannot be modified. const fields must be assigned a value at the time of declaration through-out the program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
public int a = 10;
public int constant()
{
a = 20;
return a;
}
}
class run
{
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(p.constant());
}
}
}
Output
20
Now we declare the const keyword with a variable. It will show an error.
- By default a const is static, hence you cannot define a const type as static. The following image shows the error when we use the static keyword with const.
- A const field is a compile-time constant. A const field or local variable can be initialized with a constant expression that must be fully evaluated at compile time.
2. readonly Keyword
- A readonly field can be initialized at the time of declaration.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
public readonly int a = 10; // initialized at the time of declaration
public int constant()
{
return a;
}
}
class run
{
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(p.constant());
}
}
}
Output
10
- A readonly field can be set within the constructor or this value can only be changed in the constructor. Can't be changed in normal functions.
class Program
{
public readonly int a; // initialized at the time of declaration
public Program()
{
a = 20;
}
}
- readonly fields can be used for run-time constants.
3. static keyword
- The static keyword is used to specify a static member, which means static members are common to all the objects.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static int a; // initialized at the time of declaration
public static int Hello()
{
a = 20;
return a;
}
}
class run
{
static void Main(string[] args)
{
//Program p = new Program();
Console.WriteLine(Program.Hello());
}
}
}
Output
20
Now we take a variable without declaring the static keyword.
- This 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.