R
rossum
I want to declare a const multi-line string inside a method, and I am
having some problems using Environment.NewLine.
I started out with:
class foo {
public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}
}
Trying to compile this gives me:
"Error 'multiline' is of type 'string.' A const of reference type
other than string can only be initialized with null."
Somewhat confusing, since 'multiline' _is_ of type string so the rest
of the error message does not apply. It does underline the last of
the Environment.NewLine's which is presumably what it is objecting to.
Experimenting with taking them out shows that any Environment.NewLine
will cause a failure to compile, with the last one underlined.
This string is only used inside the method, and is never changed. It
will compile as readonly outside the method, but that makes for
useless clutter at the class level. It will compile as a non-const
string, but that is not as safe as I would like it to be. It will
compile with a hard coded '\n' in the strings, but that is not as
portable as I would want it to be.
How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?
rossum
having some problems using Environment.NewLine.
I started out with:
class foo {
public void PrintStuff() {
const string multiline =
"The first line." + Environment.NewLine +
"The second line." + Environment.NewLine +
"The third line.";
Console.WriteLine(multiline);
}
}
Trying to compile this gives me:
"Error 'multiline' is of type 'string.' A const of reference type
other than string can only be initialized with null."
Somewhat confusing, since 'multiline' _is_ of type string so the rest
of the error message does not apply. It does underline the last of
the Environment.NewLine's which is presumably what it is objecting to.
Experimenting with taking them out shows that any Environment.NewLine
will cause a failure to compile, with the last one underlined.
This string is only used inside the method, and is never changed. It
will compile as readonly outside the method, but that makes for
useless clutter at the class level. It will compile as a non-const
string, but that is not as safe as I would like it to be. It will
compile with a hard coded '\n' in the strings, but that is not as
portable as I would want it to be.
How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?
rossum