It is a common interview question what is the
difference between constant and readonly and most of the candidates
can’t answer this question. Actually our problem is we just see the things and
search the code and just paste it into our program and we don’t even know how
it works? Why we are using it?
When you people focus on What, Why and how then you’ll
be successful in your career. Let’s try to understand the difference between const and readonly
Here we’ve 2 types of constants in C#. Both of them
helps you to define the constant in your application.
- · Compile Time Constant
- · Runtime Constant
Look when we build our
program it is the compile time and when the application runs it is the runtime.
Differences
- We initialize the const field at the declaration. And we can initialize the readonly field at the declaration and in the constructor as well when we are going to use but when the readonly and const variable are set then they can’t be changed into the complete program.
class Program
{
private const double PI1 = 3.14;
private readonly double PI2 = 3.1;
private readonly double PI3;
public Program()
{
PI3 = 3.2;
}
static void Main(string[] args)
{
}
}
- We use the const when the value is absolutely constant like we know 1m has 100cm and it can never be change. But we use the readonly when the value depends upon the user like PI value can be 3.1 or 3.14. So it is not absolutely constant.
- const is static by default but readonly is not static by default we need to manually make it static if we want.
namespace HelloWorld
{
class Vehicle
{
public const int NmbrOfTyres = 4;
public readonly string Name;
public Vehicle()
{
Name = "BMW";
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Vehicle.NmbrOfTyres);
Vehicle vehicle = new Vehicle();
Console.WriteLine(vehicle.Name);
Console.ReadLine();
}
}
}
We can also create the
class instance and set the readonly value conditionally.
namespace HelloWorld
{
class Vehicle
{
public const int NmbrOfTyres = 4;
public readonly string Name;
public Vehicle()
{
Name = "BMW";
}
public Vehicle(bool type)
{
Name = "Lamborgini";
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Vehicle.NmbrOfTyres);
Vehicle vehicle = new Vehicle(true);
Console.WriteLine(vehicle.Name);
Console.ReadLine();
}
}
}
- if we want to make the readonly static then we need to treat it like we do with static members.
namespace HelloWorld
{
class Vehicle
{
public static readonly string Name = "BMW";
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Vehicle.Name);
Console.ReadLine();
}
}
}
- constants can be declared at function level. But readonly must declared at class level.·
- Constants values are stored in the program
IL whereas readonly values occur at runtime so they doesn’t store in program
IL.Build the program and Open the project through ILSpy.
- As we know readonly constant value depends upon the user. So we can set the value in app.config and access it in the program.
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="price" value="25"/>
</appSettings>
</configuration>
But for this you
need to add the reference into your program of ‘System.Configuration’
class Program
{
private readonly int _price;
public Program()
{
_price = Convert.ToInt32(ConfigurationManager.AppSettings["price"]);
Console.WriteLine("Total price is: " + _price);
}
static void Main(string[] args)
{
Program program = new Program();
Console.ReadLine();
}
}
Note
Keep in your mind, once you initialize the readonly variable you can’t change its value later even during the execution of your program. And you know about const already that it limits you at compile time as well.
0 comments:
Post a Comment