There are many different keywords available in C#. This tutorial will look at the differences between the keywords readonly and const keywords in C# and when to use each.
By default, a const is static. In C#, a constant is a value that cannot be changed during the execution or run time of a program. That means value of Const will not change during whole program. It's mandatory to assign a value to it when we declared it. Using constants can improve code readability and maintainability, and can also help prevent bugs caused by accidental modification of values. Here, you will see how to declare and use a constant in C#.
Points to Remember
Example
This will declare the variable as "constant", which means unchangeable and read-only.
using System;
using System.Text;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
const double pi = 3.14159; // constant declaration That means value of that will not change.
double r;
r = Convert.ToDouble(Console.ReadLine());
double area = pi * r * r;
Console.WriteLine("Area of circle is: {0}", area);
}
}
}
Output
A Readonly field can be initialized either at the time of declaration or within the constructor of the same class. We can also change the value of a Readonly at runtime or assign a value to it at runtime (but in a non-static constructor only).
Points to Remember
Example
using System;
using System.Text;
namespace FirstProgram
{
public class TestClass
{
public readonly string ConnectionString = "TutorialsTrend";
public TestClass()
{
ConnectionString = "Differentsites"; // declared in the constructer class will compile
}
public void TestMethod()
{
ConnectionString = "NewSites";//Will not compile
}
}
}
Error on Test Method
The static keyword is used to declare a static member. If we are declare a class as a static class then in this case all the class members must be static. The static keyword can be used effectively with classes, fields, operators, events, methods etc.
Points to Remember
Example
using System;
using System.Text;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
Program.MyMethod(); // Working Fine and compile code
Program pt= new Program();
pt.MyMethod(); //Will not compile
}
public static void MyMethod()
{
Console.WriteLine("TutorialsTrend.Com");
}
}
}
Compiler will not compile that code.
Program pt= new Program();
pt.MyMethod(); //Will not compile
S.No. | Const | ReadOnly | Static |
---|---|---|---|
1 | A Const can only initialized at the time of declaration. | A ReadOnly can be initialized at the time of declaration or within the constructor of the same class. | A Static value can ve assigned at run time or compiled time and may be change at run time. |
2 | A Const field is a compile time constant. | A ReadOnly field is used as a run time constant. | A static field is used as a run time constant. |
3 | It can be access using classname.constname | It can be access using Instance | It can be access using classname.constname |
4 | Once a value is assigned to a constant. It can not be initialized again. | You can change ReadOnly value number of times. You can also change it inside constructor. | Static value can only be changed inside static constructor only. It can be changed only onece at run time. |