Click here for information on how to use code snippets in VS.NET (2005+)
// NOTE: This code snippet is designed for VS.NET 2005 or later. public class Test { // Note that constants are static by default and readonly // variables are not. // A constant, "Hello!", must be provided at the time the constant // is: public const string Hello = "Hello!"; // This constant will also work: public const int Number = 1; // This will throw an exception because DateTime.Now is NOT a public constant public const DateTime Now = DateTime.Now; // This constant will throw an error because it's not set: public const short ShortNumber; // A readonly, "Hey!", can be set at runtime or at declaration time // and doesn't need to be a literal value: public readonly String Hey1 = "Hey!"; // Since a readonly variable doesn't need to be set at declaration // time, the following will work; variable Hey2 can be set ONLY in // the constructor of the class: public readonly String Hey2; // Unlike constant, readonly variables can have non-literal values: public readonly DateTime RightNow = DateTime.Now; }
XML View:
Declaring Readonly and Constant Variables declreadonlyconst Declaring Readonly and Constant Variables. C# .Net Fun - http://www.dotnetfun.com - Michael G.