C#中的常量数组 [英] Constant arrays in C#

查看:163
本文介绍了C#中的常量数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么原因导致无法在
C#中实例化const数组或者我错过了什么


void Foo()

{

const int i = 0;

const string str =" s";


//错误CS0133:分配给''intArray''的表达式必须是

常量

const int [] intArray = {1,2,3};

const string [] strArray = {" 1"," 2"," 3" };

}


我的解决方案并不优雅,是在课堂范围内使用

静态只读字符串[ ] _strArray = {" 1"," 2"," 3" };


我无法重新初始化数组,但_strArray [0]将更改

内容。


这很容易在C ++中完成,例如。


问候

/ m

Whats the reasoning for not being able to instantiate const arrays in
C# or have I missed something

void Foo()
{
const int i = 0;
const string str = "s";

// error CS0133: The expression being assigned to ''intArray'' must be
constant
const int[] intArray = { 1,2,3 };
const string[] strArray = { "1","2","3" };
}

My solutin which isn''t as elegant is to use at class scope
static readonly string[] _strArray = { "1","2","3" };

I can''t reinitialize the array but _strArray[0] will change the
contents.

This is easy enough to do in C++ eg.

Regards
/m

推荐答案

在11/6/2003 10:40 AM,Michel Andr?写道:
On 11/6/2003 10:40 AM, Michel Andr? wrote:
是什么原因导致无法在C#中实例化const数组或者我错过了什么

void Foo()
{
const int i = 0;
const string str =" s" ;;

//错误CS0133:分配给''intArray''的表达式必须是
constant
const int [] intArray = {1,2,3};
const string [] strArray = {" 1"," 2"," 3" }
}
我的解决方案并不优雅,是在课堂范围内使用
静态readonly string [] _strArray = {" 1"," 2" ;," 3英寸我无法重新初始化数组,但_strArray [0]将更改
内容。
Whats the reasoning for not being able to instantiate const arrays in
C# or have I missed something

void Foo()
{
const int i = 0;
const string str = "s";

// error CS0133: The expression being assigned to ''intArray'' must be
constant
const int[] intArray = { 1,2,3 };
const string[] strArray = { "1","2","3" };
}

My solutin which isn''t as elegant is to use at class scope
static readonly string[] _strArray = { "1","2","3" };

I can''t reinitialize the array but _strArray[0] will change the
contents.




来自MSDN :


"常量表达式是一个表达式,可以在

编译时完全评估。因此,

引用类型常量的唯一可能值是string和null。



From MSDN:

"A constant expression is an expression that can be fully evaluated at
compile time. Therefore, the only possible values for constants of
reference types are string and null."


Bogdan Lachendro< la ***@no-spam.icslab.agh.edu.pl>在消息新闻中写道:< uf ************** @ TK2MSFTNGP09.phx.gbl> ...
Bogdan Lachendro <la***@no-spam.icslab.agh.edu.pl> wrote in message news:<uf**************@TK2MSFTNGP09.phx.gbl>...
来自MSDN:

"常量表达式是一个可以在编译时完全评估的表达式。因此,
引用类型常量的唯一可能值是string和null。
From MSDN:

"A constant expression is an expression that can be fully evaluated at
compile time. Therefore, the only possible values for constants of
reference types are string and null."




我意识到这一点并将其读取。我只是无法理解它,因为

常量对象经常使用得非常方便,因为它们不能被b / b $ b错误地改变并且它们的构造可以被优化,编译

事先。但我想这是我的C ++心态闪耀的低谷;)。我实际上也错过了const方法和对象,因为只知道没有人知道没有人会修改我拥有的对象状态克隆

以防万一有人可能改变状态或依赖用户

克隆对象如果他们改变状态他们怎么能知道什么

状态改变如果不是没有标记(我想念的另一个C ++主题)。


/ Michel



I realize this and read it to. I just cant understand it since
constant objects come in quite handy often, both since they cannot be
misstakenly change and their construction can be obtimized, compiled
beforehand. But I guess it''s my C++ mindset shining trough ;). I
actually miss const methods and objects as well, since the only way of
knowing noone will tinker with an objects state that I own is cloning
just in case somebody might change the state or depend that the user
clones the object if they change the state and how can they know what
state changes if that isn''t marked (another of those C++ isms i miss).

/Michel


这是指引用类型。它们与价值类型不同。请

检查
http://msdn.microsoft.com/library/de...harpspec_4.asp


及以下部分。他们解释了差异。


如果你想监视引用对象的变化,我认为属性

可以帮助你避免克隆。您可以随时监控来自

的来源,并在方法上使用声明或安全性。我建议

检查
http://msdn.microsoft.com/library/de...sssecurity.asp


关于const方法,你可能应该看看静态方法/

属性。如果我记得正确的是什么是const方法;-))


HTH

Alex

" Michel Andr?" < MI ********** @ swipnet.se>在留言中写道

新闻:3f ************************** @ posting.google.c om ...
This refers to reference types. They are different from value types. Please
check
http://msdn.microsoft.com/library/de...harpspec_4.asp

and following sections. They explain the difference.

If you want to monitor for changes in referenced objects, I think properties
can help you to avoid cloning. You can always monitor where from call
originates and use declarative or security on methods. I would suggest to
check
http://msdn.microsoft.com/library/de...sssecurity.asp

As about const methods, possibly you should take a look at static methods /
properties. If I remember properly what is const method ;-))

HTH
Alex
"Michel Andr?" <mi**********@swipnet.se> wrote in message
news:3f**************************@posting.google.c om...
Bogdan Lachendro< la *** @ no-spam.icslab.agh.edu.pl>在消息中写道
Bogdan Lachendro <la***@no-spam.icslab.agh.edu.pl> wrote in message



news:< uf ************** @ TK2MSFTNGP09.phx.gbl> ...


news:<uf**************@TK2MSFTNGP09.phx.gbl>...

来自MSDN:

"常量表达式是一个可以在编译时完全评估的表达式。因此,
引用类型常量的唯一可能值是string和null。
From MSDN:

"A constant expression is an expression that can be fully evaluated at
compile time. Therefore, the only possible values for constants of
reference types are string and null."



我意识到这一点并将其读取。我只是无法理解它,因为常常的物体经常非常方便,因为它们不能被错误地改变,并且它们的构造可以事先被编辑,编译。但我想这是我的C ++心态闪耀的低谷;)。我实际上也错过了const方法和对象,因为知道没有人的唯一方法是修补我拥有的对象状态是克隆
以防有人可能改变状态或依赖用户如果他们改变了状态就会克隆对象,如果没有标记,他们怎么知道状态会发生什么变化(我错过了另一个C ++主题)。

/ Michel



I realize this and read it to. I just cant understand it since
constant objects come in quite handy often, both since they cannot be
misstakenly change and their construction can be obtimized, compiled
beforehand. But I guess it''s my C++ mindset shining trough ;). I
actually miss const methods and objects as well, since the only way of
knowing noone will tinker with an objects state that I own is cloning
just in case somebody might change the state or depend that the user
clones the object if they change the state and how can they know what
state changes if that isn''t marked (another of those C++ isms i miss).

/Michel



这篇关于C#中的常量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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