Apart from the apparent difference of
- having to declare the value at the time of a definition for a
const
VS readonly
values can be computed dynamically but need to be assigned before the constructor exits.. after that it is frozen.
const
's are implicitly static
. You use a ClassName.ConstantName
notation to access them.
There is a subtle difference. Consider a class defined in AssemblyA
.
public class Const_V_Readonly
{
public const int I_CONST_VALUE = 2;
public readonly int I_RO_VALUE;
public Const_V_Readonly()
{
I_RO_VALUE = 3;
}
}
AssemblyB
references AssemblyA
and uses these values in code. When this is compiled:
- in the case of the
const
value, it is like a find-replace. The value 2 is 'baked into' the AssemblyB
's IL. This means that if tomorrow I update I_CONST_VALUE
to 20, AssemblyB
would still have 2 till I recompile it.
- 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, AssemblyB
gets the new value without recompilation. So if I_RO_VALUE
is updated to 30, you only need to build AssemblyA
and all clients do not need to be recompiled.
So if you are confident that the value of the constant won't change, use a const
.
public const int CM_IN_A_METER = 100;
But if you have a constant that may change (e.g. w.r.t. precision).. or when in doubt, use a readonly
.
public readonly float PI = 3.14;
Update: Aku needs to get a mention because he pointed this out first. Also I need to plug where I learned this: Effective C# - Bill Wagner
If you just want to pass a std::string
to a function that needs const char*
you can use
std::string str;
const char * c = str.c_str();
If you want to get a writable copy, like char *
, you can do that with this:
std::string str;
char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0'; // don't forget the terminating 0
// don't forget to free the string after finished using it
delete[] writable;
Edit: Notice that the above is not exception safe. If anything between the new
call and the delete
call throws, you will leak memory, as nothing will call delete
for you automatically. There are two immediate ways to solve this.
boost::scoped_array
boost::scoped_array
will delete the memory for you upon going out of scope:
std::string str;
boost::scoped_array<char> writable(new char[str.size() + 1]);
std::copy(str.begin(), str.end(), writable.get());
writable[str.size()] = '\0'; // don't forget the terminating 0
// get the char* using writable.get()
// memory is automatically freed if the smart pointer goes
// out of scope
std::vector
This is the standard way (does not require any external library). You use std::vector
, which completely manages the memory for you.
std::string str;
std::vector<char> writable(str.begin(), str.end());
writable.push_back('\0');
// get the char* using &writable[0] or &*writable.begin()
Best Answer
public static readonly
fields are a little unusual;public static
properties (with only aget
) would be more common (perhaps backed by aprivate static readonly
field).const
values are burned directly into the call-site; this is double edged:If the value will never change, then const is fine -
Zero
etc make reasonable consts ;p Other than that,static
properties are more common.