[c#] Adding line break in C# Code behind page

I have written a code in C# which is exceeding page width, so i want it to be broken into next line according to my formatting. I tried to search a lot to get that character for line break but was not able to find out.

In VB.NET I use '_' for line break, same way what is used in C# ? I am trying to break a string.

Thanks in Advance Shantanu Gupta

This question is related to c#

The answer is


If I am understanding this correctly, you should be able to break the string into substrings to accomplish this.

i.e.:

string s = "this is a really long string" +
"and this is the rest of it";

guys.. use resources for long strings in code behind!!

also.. you don't need an _ for codeline breaks in C#. In VB the codelines end with a newline character (or a ':'), using the the _ would tell the parser it has not reached the end of the line yet. The codeline in C# ends with a ';' so you can use newlines to styleformat your code.


C# code can be split between lines on pretty much any syntatic construct without a need for a '_' style construct.

For example

foo.
 Bar(
   42
 , "again");

Use @ symbol before starting the string. like

string s = @"this is a really
long string
and this is 
the rest of it";

Strings are immutable, so using

public string GenerateString()
{
    return
        "abc" +
        "def";
}

will slow you performance - each of those values is a string literal which must be concatenated at runtime - bad news if you reuse the method/property/whatever alot...

Store your string literals in resources is a good idea...

public string GenerateString()
{
    return Resources.MyString;
}

That way it is localisable and the code is tidy (although performance is pretty terrible).


   dt = abj.getDataTable(
"select bookrecord.userid,usermaster.userName, "
                                                +" book.bookname,bookrecord.fromdate, "
                                                +" bookrecord.todate,bookrecord.bookstatus "
                                                +" from book,bookrecord,usermaster " 
                                                +" where bookrecord.bookid='"+ bookId +"' "
                                                +" and usermaster.userId=bookrecord.userid "
                                                +" and book.bookid='"+ bookId +"'");

In C# there's no 'new line' character like there is in VB.NET. The end of a logical 'line' of code is denoted by a ';'. If you wish to break the line of code over multiple lines, just hit the carriage return (or if you want to programmatically add it (for programmatically generated code) insert 'Environment.NewLine' or '\r\n'.

Edit: In response to your comment: If you wish to break a string over multiple lines (i.e. programmatically), you should insert the Environment.NewLine character. This will take the environment into account in order to create the line ending. For instance, many environments, including Unix/Linux only use a NewLine character (\n), but Windows uses both carriage return and line feed (\r\n). So to break a string you would use:

string output = "Hello this is my string\r\nthat I want broken over multiple lines."

Of course, this would only be good for Windows, so before I get flamed for incorrect practice you should actually do this:

string output = string.Format("Hello this is my string{0}that I want broken over multiple lines.", Environment.NewLine);

Or if you want to break over multiple lines in your IDE, you would do:

string output = "My string"
              + "is split over"
              + "multiple lines";

 result = "Minimum MarketData"+ Environment.NewLine
           + "Refresh interval is 1";

All you need to do is add \n or to write on files go \r\n.

Examples:

say you wanted to write duck(line break) cow this is how you would do it Console.WriteLine("duck\n cow");

Edit: I think I didn't understand the question. You can use

@"duck
cow".Replace("\r\n", "")

as a linebreak in code, that produces \r\n which is used Windows.


C# doesn't have an explicit line break character. You statements end with a semicolon so you can span your statements over many lines. These are both the same:

public string GenerateString()
{
    return "abc" + "def";
}

public string GenerateString()
{
    return
        "abc" +
        "def";
}