C#'s readonly keyword can be applied to a field so that it can only be initialized during construction (either a static or an instance constructor), but without making it const. This is very useful to simulate const fields for types that cannot be initialized at compile time (i.e. non-primitive types).
Besides the compiler behavior, readonly has the C# compiler emit the initonly metadata attribute on the field declaration.
In MC++, readonly was not supported, but C++/CLI fully supports these semantics through the use of the initonly keyword. Here's an example:
ref struct Keyword
{
public:
property String^ Name;
Keyword(String^ name)
{
Name = name;
}
};
ref class Keywords sealed abstract
{
public:
static initonly Keyword^ New = gcnew Keyword("new");
static initonly Keyword^ Dispose = gcnew Keyword("dispose");
};
int main()
{
Console::WriteLine(Keywords::New->Name);
Console::WriteLine(Keywords::Dispose->Name);
}
.: tomasr | 2004-10-10 18:51:24 :.