c# - Why can I not assign the concatenation of constant strings to a constant string? -
occasionally want break apart constant string formatting reasons, sql.
const string select_sql = "select field1, field2, field3 table1 field4 = ?";
to
const string select_sql = "select field1, field2, field3 " + "from table1 " + "where field4 = ?";
however c# compiler not allow second form constant string. why?
um, should fine... sure doesn't compile?
sample code:
using system; class test { const string myconstant = "foo" + "bar" + "baz"; static void main() { console.writeline(myconstant); } }
my guess in real code you're including non-constant expression in concatenation.
for example, fine:
const string myfield = "field"; const string sql = "select " + myfield + " table";
but isn't:
static readonly string myfield = "field"; const string sql = "select " + myfield + " table";
this attempting use non-constant expression (myfield
) within constant expression declaration - , that's not permitted.
Comments
Post a Comment