C #: How do you construct a multi-line string at design time?
If I understand your question correctly, you need the @ sign in front of your line. This will force the compiler to take your string literally (including newlines, etc.)
In your case, I would write the following:
String sDescription =
@"Options:
-t Description of -t argument.";
So far for your question (hopefully), but I would suggest just using multiple WriteLines. The performance penalty costs next to nothing, and it's just more adaptable. You can work with a format string so you can do this:
string formatString = "{0:10} {1}";
Console.WriteLine("Options:");
Console.WriteLine(formatString, "-t", "Description of -t argument.");
Console.WriteLine(formatString, "-b", "Description of -b argument.");
formatstring makes sure your strings are formatted nicely without manually putting spaces in, and make sure that if you ever want to change the format, you just need to do it in one place.
source to share
Console.Write("Options:\n\tSomething\t\tElse");
produces
Options:
Something Else
\ n for next line, \ t for tab, for more professional layouts try setting margin width using format specifiers. http://msdn.microsoft.com/en-us/library/txafckwd.aspx
source to share
Personally, I usually just write three calls to Console.WriteLine. I know this gives extra fluff, but it aligns the text appropriately and makes sure it uses the correct line terminator for whatever platform I'm running on. An alternative would be to use a shorthand literal, but this would "fix" the line terminator at compile time.
source to share
I know C # is mostly used on Windows machines, but please please try to write your code as platform neutral. Not all platforms have the same end-of-line character. To get the line end character correctly for the current executable platform, you should use:
System.Environment.NewLine
Maybe I'm just anal because I'm a former Java programmer who has run applications on many platforms, but you never know what the platform of the future is.
source to share
The "best" answer depends on where the information you are showing comes from.
If you want to hard-code it, using the "@" line is very efficient, although you will find that when displayed on the right it plays a hilarious code-formatted hell.
For a more substantial piece of text (more than two lines) a good use of text resources.
But if you need to build a string on the fly, say by navigating the command line options supported by your application, then you should examine both StringBuilder and Format Strings.
StringBuilder has methods such as AppendFormat () that accept format strings, making it easier to create format strings.
Format strings make it easy to combine multiple elements together. Note that formatted strings can be used to format objects to a specific width.
To quote the MSDN page linked above:
Format Element Syntax
Each format element takes the following forms and consists of the following components:
{index [, alignment] [: FormatString]}
Matching curly braces ("{" and "}") are required.
Index component
The required component of the index, also called the parameter specifier, is a number starting with 0 that identifies the corresponding element in the object list ...
Alignment component
The optional alignment component is an integer indicating the preferred formatted field width. If the alignment value is less than the formatted string length, the alignment is ignored, and the formatted string length is used as the field width. Formatted data in a field is right-aligned if the alignment is positive and left-aligned if the alignment is negative. A space is used if padding is required. a comma is required if alignment is specified.
String Formatting Component
The optional formatString component the appropriate format string for the type of object being formatted ...
source to share