声明一个常量 [英] Declaring a constant

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

问题描述

我做了这样的声明:


private const Complex I = new Complex(0.0,1.0);


当我尝试构建这个我得到错误:


分配给''ComplexNumberLib.ComplexMath.I'的表达式必须是

常量。


我不明白为什么这个构造函数不被认为是常量

以及如何更正

来纠正这个错误。 (刚刚学习C#)


问候

Chris Saunders

解决方案

你好Chris,

a常量表达式需要在

编译时进行评估,因此您只能使用值类型的consts而不是引用类型

(除了字符串有点不同并且为空)。


如果你想要一个不可更改的引用类型,请使用readonly关键字

相反。


希望有所帮助

Mark Dawson
http://www.markdawson.org


" Chris Saunders"写道:

我做了这样的声明:

私人const复杂I =新复杂(0.0,1.0);

当我尝试构建它时,我收到错误:

分配给''ComplexNumberLib.ComplexMath.I'的表达式必须是常量。

我不明白为什么这个构造函数不被认为是常量
也不是如何纠正这个错误。 (刚刚学习C#)

问候
Chris Saunders



Chris,
< blockquote class =post_quotes>我做了这样的声明:

private const Complex I = new Complex(0.0,1.0);

当我尝试构建这个时我收到错误:

分配给''ComplexNumberLib.ComplexMath.I'的表达式必须是常量。




常量表达式是一个可以在
编译时完全评估的表达式。因此,参考常量

类型的唯一可能值是字符串和null。


问候,


Randy


您是否为Complex编写了代码?如果是这样,你能为你打电话的

构造函数提供代码吗?

-

Dale Preston

MCAD C#

MCSE,MCDBA

" Chris Saunders"写道:

我做了这样的声明:

私人const复杂I =新复杂(0.0,1.0);

当我尝试构建它时,我收到错误:

分配给''ComplexNumberLib.ComplexMath.I'的表达式必须是常量。

我不明白为什么这个构造函数不被认为是常量
也不是如何纠正这个错误。 (刚刚学习C#)

问候
Chris Saunders



I have made a declaration like this:

private const Complex I = new Complex(0.0, 1.0);

When I try to build this I get the error:

The expression being assigned to ''ComplexNumberLib.ComplexMath.I'' must be
constant.

I do not understand why this constructor is not considered to be constant
nor how
to correct this error. (Just learning C#)

Regards
Chris Saunders

解决方案

Hi Chris,
a constant expression needs to be something that can be evaluated at
compile time, so you can only use consts with value types not reference types
(apart from string which is a little bit different and null).

If you want to have a non changeable reference type use the readonly keyword
instead.

Hope that helps
Mark Dawson
http://www.markdawson.org

"Chris Saunders" wrote:

I have made a declaration like this:

private const Complex I = new Complex(0.0, 1.0);

When I try to build this I get the error:

The expression being assigned to ''ComplexNumberLib.ComplexMath.I'' must be
constant.

I do not understand why this constructor is not considered to be constant
nor how
to correct this error. (Just learning C#)

Regards
Chris Saunders



Chris,

I have made a declaration like this:

private const Complex I = new Complex(0.0, 1.0);

When I try to build this I get the error:

The expression being assigned to ''ComplexNumberLib.ComplexMath.I'' must be
constant.



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.

Regards,

Randy


Did you write the code for Complex? If so, can you provide the code for the
constructor you''re calling?
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Chris Saunders" wrote:

I have made a declaration like this:

private const Complex I = new Complex(0.0, 1.0);

When I try to build this I get the error:

The expression being assigned to ''ComplexNumberLib.ComplexMath.I'' must be
constant.

I do not understand why this constructor is not considered to be constant
nor how
to correct this error. (Just learning C#)

Regards
Chris Saunders



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

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