Diff b/w Static,Constant and Readonly

Static: Value can be initialized once
Constant: Value can not be changed
initialization must be at compile time
Readonly: Value can be initialized only once from the constructor of a class
initialization can be at run time
EX: 
class Manager
{
    readonly DateTime _startup; // the readonly field

    public Manager()
    {
 // Initialize startup time.
 this._startup = DateTime.Now;
    }

    public DateTime GetStartup()
    {
 // We cannot modify the DateTime here.
 return this._startup;
    }
}
class Program
{
    static void Main()
    {
 Manager manager = new Manager();
 Console.WriteLine(manager.GetStartup());
    }
} 
 
OutPut:
Current time  



No comments:

Post a Comment