Const:
1. Compile time only const property value will set. It will not change at runtime.
2. Bascially, constants are static in nature.
3. we should declare constants with in the funcations.
4. We can use this one as attribute.
5. When you refer constant from one assembly to other, value will compiled into the calling assembly. If you try to update the constant value in the referenced assembly it won't change.
6. Const can't be marked as static.
Readonly:
1. At Runtime readonly field will evaluate.
2. We can change the readonly value at runtime. But we can't change the structure of the array. Please check below example 2.It will mark the field as unchangeable.
3. It will not execute at compile time
3. Most of the time we will use this readonly in the constructors for assignments. Please refer example1 and image.
Example:1
Example:2
class ReadonlyChangeMethod
{
public const string CONSTANTVALUE = "Ranga RAjesh";
public readonly string[] TOWNS = new string[] { "HYDERABAD", "BANGALORE" };
public string UpdateReadonlyField()
{
TOWNS[0] = "Mumbai";
return TOWNS[0];
}
}
class ReadonlyVsConst
{
static void Main(string[] args)
{
ReadonlyChangeMethod obj = new ReadonlyChangeMethod();
Console.WriteLine(obj.UpdateReadonlyField());
Console.ReadLine();
}
}
//Output: Mumbai
If you have any queries or suggestions, please fell free to ask in comments section.
1. Compile time only const property value will set. It will not change at runtime.
2. Bascially, constants are static in nature.
3. we should declare constants with in the funcations.
4. We can use this one as attribute.
5. When you refer constant from one assembly to other, value will compiled into the calling assembly. If you try to update the constant value in the referenced assembly it won't change.
6. Const can't be marked as static.
Readonly:
1. At Runtime readonly field will evaluate.
2. We can change the readonly value at runtime. But we can't change the structure of the array. Please check below example 2.It will mark the field as unchangeable.
3. It will not execute at compile time
3. Most of the time we will use this readonly in the constructors for assignments. Please refer example1 and image.
Example:1
public class ConstAndReadOnly
{
public const int iVALUE = 6;
public readonly int iReadOnly;
public ConstAndReadOnly()
{
iReadOnly= 3;
}
}
Example:2
class ReadonlyChangeMethod
{
public const string CONSTANTVALUE = "Ranga RAjesh";
public readonly string[] TOWNS = new string[] { "HYDERABAD", "BANGALORE" };
public string UpdateReadonlyField()
{
TOWNS[0] = "Mumbai";
return TOWNS[0];
}
}
class ReadonlyVsConst
{
static void Main(string[] args)
{
ReadonlyChangeMethod obj = new ReadonlyChangeMethod();
Console.WriteLine(obj.UpdateReadonlyField());
Console.ReadLine();
}
}
//Output: Mumbai
Difference between readonly and const in c# |
If you have any queries or suggestions, please fell free to ask in comments section.
Post a Comment
Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.