Occasionally I want to break apart a constant string for formatting reasons, usually SQL.
const string SELECT_SQL = "SELECT Field1, Field2, Field3 FROM TABLE1 WHERE Field4 = ?";
to
const string SELECT_SQL = "SELECT Field1, Field2, Field3 "
+ "FROM TABLE1 "
+ "WHERE Field4 = ?";
However the C# compiler will not allow this second form to be a constant string. Why?
Um, that should be fine… are you sure it doesn’t compile?
Sample code:
My guess is that in your real code you’re including some non-constant expression in the concatenation.
For example, this is fine:
but this isn’t:
This is attempting to use a non-constant expression (
MyField) within a constant expression declaration – and that’s not permitted.