\n in C#?

J

James

Hi all,

Probably the first thing most of you learn, but I don't know the
newline character for C#.

Thanks,
James
 
J

jeremiah johnson

string newline = System.Environment.NewLine;

That's how you retrieve it for whatever system you're running on.
Remember, your .net code can sometimes run on linux systems (or any unix
at all) using Mono. If you want to print a line without a newline
after, use:

Console.Write("blahblah");

If you want to print a line to the screen FOLLOWED by a newline
(independent of the system) use:

Console.WriteLine("bbllaahh");

This will work cross platform, as well as the first line of code above.
Don't hardcode in specific newline sequences such as \n, \r\n, or \r.
 
J

Jon Skeet [C# MVP]

This will work cross platform, as well as the first line of code above.
Don't hardcode in specific newline sequences such as \n, \r\n, or \r.

It depends on what you're doing. If you're writing to files which want
the system newline, then you should indeed use WriteLine and/or
Environment.NewLine.

If, however, you're dealing with a network protocol, you absolutely
*should* use \r, \n or \r\n, because the protocol will (or at least
should) define what *it* thinks is the the newline. At that point, if
you use Environment.NewLine you *will* have a bug on one platform or
other.
 
R

rossum

Hi all,

Probably the first thing most of you learn, but I don't know the
newline character for C#.

Thanks,
James

Try either \r or \r\n.

rossum



The ultimate truth is that there is no ultimate truth
 
J

JS

Probably the first thing most of you learn, but I don't know the
newline character for C#.

The newline character is O/S dependent. I would use the
Environment.NewLine, as others have already suggested. However, if you are
determined to use a character sequence...

\r = newline for macintosh
\r\n = newline for windows
\n = newline for UNIX based systems

....the NewLine that everone mentioned will determine the O/S and use the
appropriate value for that O/S.
 
M

Michael A. Covington

JS said:
The newline character is O/S dependent. I would use the
Environment.NewLine, as others have already suggested. However, if you
are
determined to use a character sequence...

\r = newline for macintosh
\r\n = newline for windows
\n = newline for UNIX based systems

This is the kind of thing that ought to be built into the language as
something more concise than Environment.NewLine. Maybe define \N as the
OS-dependent newline, known to the compiler.
 
J

Jon Skeet [C# MVP]

Michael A. Covington said:
This is the kind of thing that ought to be built into the language as
something more concise than Environment.NewLine. Maybe define \N as the
OS-dependent newline, known to the compiler.

No - precisely because it's *not* known to the compiler. The compiler
doesn't know what platform the code is going to *run* on, it only knows
the platform it's being *compiled* on.
 
S

Scott Coonce

Mmm Jon,

Pardon my confusion, but was interested in such a long thread about such a
simple topic (at first glance). Reflector gives the following code:

public static string get_NewLine()
{
return "\r\n";
}

Isn't this the compiler hard coding the newline string?

Scott
 
J

Jon Skeet [C# MVP]

Scott Coonce said:
Pardon my confusion, but was interested in such a long thread about such a
simple topic (at first glance). Reflector gives the following code:

public static string get_NewLine()
{
return "\r\n";
}

Isn't this the compiler hard coding the newline string?

Absolutely not, in terms of the compiler of a program which *uses*
Environment.NewLine - it's the runtime which you're using on Windows
hard-coding it. A runtime on Linux might have an implementation of:

public static string get_NewLine()
{
return "\n";
}

So a program like the following, compiled on Windows but run on Linux,
would print out 1 rather than 2:

using System;

public class Test
{
static void Main()
{
Console.WriteLine (Environment.NewLine.Length);
}
}
 
K

Kevin Spencer

I write a lot of software that interacts with older systems, Unix servers,
and the like (U.S. government agencies), using protocols like FTP, UDP, and
Telnet. Once you step out of the Microsoft sandbox, you're in a world of
trouble, as JS pointed out. A newline isn't a newline when you move data
across different platforms. I have even had to write code that inserts
"\r\r\n" at the end of a line, due to a proprietary (and probably ancient)
format in use by the U.S. National Weather Service's METAR data FTP server.

So, the real answer is, inside the Microsoft sandbox, Environment.Newline is
fine. "\r\n" is fine. When interacting with other systems, a little research
is always in order. I have also found out that when receiving data from
other sources, you often have to watch out for default .Net conversions.
Sometimes the platform is a bit too automatic for my own good! Still, it has
built-in mechanisms to deal with these sorts of cases, but you do have to
watch out for them.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
M

Michael A. Covington

Jon Skeet said:
No - precisely because it's *not* known to the compiler. The compiler
doesn't know what platform the code is going to *run* on, it only knows
the platform it's being *compiled* on.

OK, then, the compiler should insert some code to insert the correct value
at run time.
 
J

Jon Skeet [C# MVP]

Michael A. Covington said:
OK, then, the compiler should insert some code to insert the correct value
at run time.

It certainly *could* do that - but that would have significant
consequences in terms of literals and interning, without pretty tricky
bits of magic going on. I prefer to keep it simple as far as the
language is concerned, and let the developer specify
Environment.NewLine if they want to. The idea of a *literal* meaning
different things on different platforms doesn't appeal to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top