多行常量字符串和Environment.Newline的问题 [英] Problem with multi-line const strings and Environment.Newline

查看:105
本文介绍了多行常量字符串和Environment.Newline的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在方法中声明一个const多行字符串,而且我使用Environment.NewLine时遇到了一些问题。


我开始了out with:


class foo {


public void PrintStuff(){

const string multiline =

第一行。 + Environment.NewLine +

第二行。 + Environment.NewLine +

"第三行。" ;;

Console.WriteLine(多行);

}


}


尝试编译这个给了我:


"错误''multiline''是类型''字符串。''引用类型的const
除了字符串之外的
只能用null初始化。


有点混乱,因为''多行''_is_类型为字符串,因此其余的

错误消息不适用。它确实强调了环境.NewLine'的最后一个,它可能是它所反对的。

尝试将它们取出显示任何Environment.NewLine

将导致编译失败,最后一个加下划线。


此字符串仅在方法内部使用,并且永远不会更改。这个
将在方法之外编译为readonly,但这使得

在类级别无用的混乱。它将编译为非const

字符串,但这并不像我希望的那样安全。它会在字符串中使用硬编码的''\ n''进行编译,但这并不像我希望的那样可移植。

。 />

如何在方法

中声明一个不变的多行字符串,同时仍然使用Environment.NewLine作为行分隔符?


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

推荐答案

我可能是老式的,但你不能打败旧的\ n,在我看来:


class foo {


public void PrintStuff(){

const string multiline =

第一行。 +\ n +

第二行。 +\ n +

第三行。;

Console.WriteLine(多行);

}

}


它对我有用。

彼得

" rossum" < ro ****** @ coldmail.com写信息

新闻:31 ************************ ******** @ 4ax.com ...
I may be old fashioned, but you just can''t beat the old "\n", in my view:

class foo {

public void PrintStuff() {
const string multiline =
"The first line." + "\n" +
"The second line." + "\n" +
"The third line.";
Console.WriteLine(multiline);
}
}

It works for me.
Peter
"rossum" <ro******@coldmail.comwrote in message
news:31********************************@4ax.com...

>我想在方法中声明一个const多行字符串,我就是

使用Environment.NewLine时遇到一些问题。


我开始时:


class foo {


public void PrintStuff(){

const string multiline =

"第一行。 + Environment.NewLine +

第二行。 + Environment.NewLine +

"第三行。" ;;

Console.WriteLine(多行);

}


}


尝试编译这个给了我:


"错误''multiline''是类型''字符串。''引用类型的const
除了字符串之外的
只能用null初始化。


有点混乱,因为''多行''_is_类型为字符串,因此其余的

错误消息不适用。它确实强调了环境.NewLine'的最后一个,它可能是它所反对的。

尝试将它们取出显示任何Environment.NewLine

将导致编译失败,最后一个加下划线。


此字符串仅在方法内部使用,并且永远不会更改。这个
将在方法之外编译为readonly,但这使得

在类级别无用的混乱。它将编译为非const

字符串,但这并不像我希望的那样安全。它会在字符串中使用硬编码的''\ n''进行编译,但这并不像我希望的那样可移植。

。 />

如何在方法

中声明一个不变的多行字符串,同时仍然使用Environment.NewLine作为行分隔符?


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





rossum写道:

< snip> ;

rossum wrote:
<snip>

如何在仍然使用Environment.NewLine作为行分隔符的情况下在方法

中声明一个不变的多行字符串?
How can I declare an unchanging multi-line string inside a method
while still using Environment.NewLine as the line separator?



简答:你不能。 Environment.NewLine是一个属性,而不是

常量,所以使用它作为常量声明的一部分

涉及编译器必须调用运行时属性在

编译时,这是不可能的。 C#编译器不会生成代码

来生成const对象;如果它不能在编译时计算它们那么

吧barfs。

The short answer: you can''t. Environment.NewLine is a property, not a
constant, so using it as part of the declaration of a constant would
involve the compiler having to invoke a run-time property at
compile-time, which it can''t do. The C# compiler doesn''t generate code
to produce const objects; if it can''t compute them at compile time then
it barfs.


嘿,


您也可以取消换行。只需这样做:


const string multiline =

@"第一行。

第二行。

第三行。;

周五,2006年12月1日17:06:49 +0000,rossum< ro ****** @ coldmail.com>

写道:
Hey,

You can do away with the newlines too. Just do this:

const string multiline =
@"The first line.
The second line.
The third line.";
On Fri, 01 Dec 2006 17:06:49 +0000, rossum <ro******@coldmail.com>
wrote:

>我想在方法中声明一个const多行字符串,我有一些使用Environment.NewLine的问题。

我开始时:

class foo {


public void PrintStuff(){

const string multiline =

"第一行。 + Environment.NewLine +

第二行。 + Environment.NewLine +

"第三行。" ;;

Console.WriteLine(多行);

}

}

试图编译这个让我:

错误'''multiline''的类型为''string。''引用类型的const
除了字符串之外只能用null初始化。

有点令人困惑,因为''multiline''_is_是字符串类型所以错误信息的其余部分没有应用。它确实强调了环境.NewLine的最后一个,这可能是它所反对的。
尝试将它们拿出来表明任何Environment.NewLine
都会导致失败编译,最后一个加下划线。

此字符串仅在方法内部使用,并且永远不会更改。它将在方法之外编译为readonly,但这会在类级别上造成无用的混乱。它将编译为非const
字符串,但这并不像我希望的那样安全。它将在字符串中用硬编码的''\ n''进行编译,但这并不像我希望的那样便携。

怎么能我在方法中声明一个不变的多行字符串
同时仍然使用Environment.NewLine作为行分隔符?

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



-


Bits.Bytes。
http ://bytes.thinkersroom.com


这篇关于多行常量字符串和Environment.Newline的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆