I have read about the const and readonly keyowrds in C#. One of the difference between these keywords is that the value of the const is resolved at compile time while readonly keyword is resolved at run time. Though I didn't get chance to implement them in any of my projects. So I thought of giving it a try. I created mylibrary which I would use in my other project. The code in mylibrary is as following
namespace MyLibrary
{
public class Class1
{
public static readonly string MyVar = "Vikram";
//public readonly string MyVar;
//public Class1()
//{
// MyVar = "Test";
//}
}
}
Now I would use this library as reference in my other console project as following
class Program
{
static void Main(string[] args)
{
//MyLibrary.Class1 class1 = new MyLibrary.Class1();
Console.WriteLine(MyLibrary.Class1.MyVar); // output is vikram
Console.ReadLine();
Console.WriteLine(MyLibrary.Class1.MyVar); // changed to Test but still op as vikram
Console.ReadLine();
}
}
Now between these two writeline's I will change the value of "MyVar" in my library and recompile it, according to readonly's concept it should get reflected in the console project without recompiling my main project.
Please correct me if I my understanding of the readonly keyword is wrong.
P.S I have been through the answers of this link. In one of the answers the following is being mentioned
in the case of the readonly value, it is like a ref to a memory location. The value is not baked into AssemblyB's IL. This means that if the memory location is updated, Assembly B gets the new value without recompilation. So if I_RO_VALUE is updated to 30, you only need to build AssemblyA. All clients do not need to be recompiled.
Can anyone please explain what does the bold line exactly means. I think this is what I am doing in my example.