Environment.Newline

P

Peter Kirk

Hi

I would like to ask a little bit about the value Environment.Newline: what
is it and what is the point of it? Ok, I can see in the docs that it
represents "newline" for the current platform - I assume that it is a
runtime property, and not compile time?

But won't it always be the same anyway - does dotnet run on anything other
than windows platforms? So isn't newline always the same?

And what if I have a program that uses "Environment.Newline" to generate a
string and send it to a program running on another platform - the other
platform might have a completely different "newline" concept, and therefore
to use Environment.Newline is pointless isn't it?

Thanks for any comments,
Peter
 
J

jeremiah johnson

C# implementations do not always run on Windows, no. Google for
"rotor", "mono", and "dotgnu" individually and you'll find examples of
non-Windows implementations of the C# language, compiler, and runtime
environment. as far as I know, the mono project is the furthest
advanced. they have a complete 1.1 environment written, and its all
open source. it compiles and runs anywhere Unix or Windows does.

Also, should it be the responsibility of the programmer to know exactly
what the newline character is on any given system? personally, I don't
think it should be. True, there are only two types of newline currently
if you only count the latest PC operating systems, and hopefully that
narrows to one in the future, but should it be the responsibility of the
programmer to determine what operating system he is on and write logic
that outputs the currect system newline?

jeremiah
 
P

Peter Kirk

jeremiah johnson said:
C# implementations do not always run on Windows, no. Google for "rotor",
"mono", and "dotgnu" individually and you'll find examples of non-Windows
implementations of the C# language, compiler, and runtime environment. as
far as I know, the mono project is the furthest advanced. they have a
complete 1.1 environment written, and its all open source. it compiles
and runs anywhere Unix or Windows does.

Also, should it be the responsibility of the programmer to know exactly
what the newline character is on any given system? personally, I don't
think it should be. True, there are only two types of newline currently
if you only count the latest PC operating systems, and hopefully that
narrows to one in the future, but should it be the responsibility of the
programmer to determine what operating system he is on and write logic
that outputs the currect system newline?

OK, I wasn't aware that .net applications could run on other platforms -
that's great. In that case I agree it is good to have a runtime property for
such things as newline (which can vary from platform to platform).

But how do you cope with sending messages from one application to another -
where we don't know what platforms the applications are running on?

For example, my application running on a windows machine generates a string
like "hello\r\n" (where "\r\n" is the Environment.Newline), and sends this
string to another application which just happens to have been installed on a
unix machine. This machine only uses "\n" characters as newline - won't
there be problems when it sees "\r\n"? Likewise, the application on the unix
machine could send a string "hello\n" (where "\n" is Environment.Newline on
unix) to the application running on windows - the application on windows
expects "\r\n".


Peter
 
A

Adam Clauss

Peter Kirk said:
OK, I wasn't aware that .net applications could run on other platforms -
that's great. In that case I agree it is good to have a runtime property
for such things as newline (which can vary from platform to platform).

But how do you cope with sending messages from one application to
another - where we don't know what platforms the applications are running
on?

For example, my application running on a windows machine generates a
string like "hello\r\n" (where "\r\n" is the Environment.Newline), and
sends this string to another application which just happens to have been
installed on a unix machine. This machine only uses "\n" characters as
newline - won't there be problems when it sees "\r\n"? Likewise, the
application on the unix machine could send a string "hello\n" (where "\n"
is Environment.Newline on unix) to the application running on windows -
the application on windows expects "\r\n".

That's basically correct. And until the world decides on a single form of
newline, we still have to handle this situation ourselves. I believe that
is why most internet protocols either define what sort of newline will be
used or do not use a newline at all - they cannot depend on the OS
implementations to be consistent.
 
N

naikrovek

cross-platform newlines are handled usually by XML. XML parsers
completely ignore whitespace, so newlines of any kind are just not paid
attention to.

that's how i handle cross-platform text transfer, anyway. i wrap it up
in lightweight xml in a way that makes each new line a seperate tag,
and send it that way.

i'm sure there are other ways to handle this. if you control the
applications on both ends of the connection you could simply code-in an
agreed upon character that represents newlines and honor that.
 

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