如何使用对象数据类型声明const字段 [英] how to declare a const field with object data type

查看:69
本文介绍了如何使用对象数据类型声明const字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一些对象类型的常量字段(除了

值类型)


公共类TestData10


{


公共TestData10(字符串名称)


{}


公共字符串机场;


}


public const TestData10 c_s1 = new TestData10(Sea);


我在最后一行收到错误,右边不是const

表达式,任何想法提示。


谢谢,


Sajjad

I need to create some constant fields those are of object type (other than
value types)

public class TestData10

{

public TestData10(string name)

{}

public string Airport;

}

public const TestData10 c_s1=new TestData10("Sea");

I am getting error on last line that right hand side is not const
expression, any idea hint.

Thanks,

Sajjad

推荐答案



const是某种存在的东西编译时的值。

因此new myObject(...)永远不会工作。

而不是const,我建议使用readonly在这种情况下。这种类型的

变量只能在声明时或者在

对象构造函数中分配一次。


希望有帮助。


Daniel。


" Sajjad Akhter" < sa@nospam.nospam>在消息中写道

新闻:ez ************** @ TK2MSFTNGP10.phx.gbl ...

A const is something that exists as some sort of value at compile time.
Therefore "new myObject(...)" would never work.
Instead of const, I''d recommend using "readonly" in this case. This type of
variable can be assigned only once, either when it''s declared, or in the
object constructor.

Hope that Helps.

Daniel.

"Sajjad Akhter" <sa@nospam.nospam> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
我需要创建一些常量字段是对象类型(除了值类型)

公共类TestData10

公共TestData10(字符串名称) )

{}

公共字符串机场;

}
公共const TestData10 c_s1 =新的TestData10("海);

我在最后一行得到错误,右手边不是const
表达,任何想法暗示。

谢谢,

Sajjad
I need to create some constant fields those are of object type (other than
value types)

public class TestData10

{

public TestData10(string name)

{}

public string Airport;

}

public const TestData10 c_s1=new TestData10("Sea");

I am getting error on last line that right hand side is not const
expression, any idea hint.

Thanks,

Sajjad



<" Dan Bass" < danielbass [at] postmaster [dot] co [dot] uk>>写道:
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
const是在编译时作为某种值存在的东西。
因此new myObject(...)永远不会工作。
而不是const,我建议使用readonly。在这种情况下。这种类型的
变量只能在声明时或在
对象构造函数中分配一次。
A const is something that exists as some sort of value at compile time.
Therefore "new myObject(...)" would never work.
Instead of const, I''d recommend using "readonly" in this case. This type of
variable can be assigned only once, either when it''s declared, or in the
object constructor.




注意,但是,这只会使变量的值(即

参考)只读。它并没有阻止你改变对象内部的价值。例如,你可以有一个readonly StringBuilder

变量,但仍然可以调用Append就可以了。


-

Jon Skeet - < sk *** @ pobox.com>
http:// www .pobox.com / ~siget

如果回复小组,请不要给我发邮件



Note, however, that that only makes the value of the variable (i.e. a
reference) readonly. It doesn''t stop you from changing values inside
the object. For instance, you could have a readonly StringBuilder
variable, but still call Append on it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


声明readonly无法解决我的问题不是const

无论使用什么方式都可能是价值类型


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Declaring readonly doesnt solve my problem as its not const
Is ther any way to use may be value types

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<" Dan Bass" < danielbass [at] postmaster [dot] co [dot] uk>>写道:
<"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
const是在编译时作为某种值存在的东西。
因此new myObject(...)永远不会工作。
而不是const,我建议使用readonly。在这种情况下。这种类型的
变量只能在声明时或在
对象构造函数中分配一次。
A const is something that exists as some sort of value at compile time.
Therefore "new myObject(...)" would never work.
Instead of const, I''d recommend using "readonly" in this case. This type
of
variable can be assigned only once, either when it''s declared, or in the
object constructor.



注意,但是,这只会使变量的值(即
参考)只读。它并没有阻止你改变对象内部的价值观。例如,你可以有一个readonly StringBuilder
变量,但仍然可以在其上调用Append。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet <如果回复小组,请不要给我发邮件



Note, however, that that only makes the value of the variable (i.e. a
reference) readonly. It doesn''t stop you from changing values inside
the object. For instance, you could have a readonly StringBuilder
variable, but still call Append on it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于如何使用对象数据类型声明const字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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