如果只给它们分配null,为什么允许将const与引用类型一起使用? [英] Why are we allowed to use const with reference types if we may only assign null to them?

查看:132
本文介绍了如果只给它们分配null,为什么允许将const与引用类型一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题实际上非常简单。下面的代码在其下方引发异常:

The question is actually very straightforward. The following code throws the exception right below it:

class Foo
{
    public const StringBuilder BarBuilder = new StringBuilder();
    public Foo(){

    }
}

错误:


Foo.BarBuilder'类型为'System.Text.StringBuilder'。 MSDN说,引用类型(不是字符串)的const字段
只能用
null初始化。

Foo.BarBuilder' is of type 'System.Text.StringBuilder'. A const field of a reference type other than string can only be initialized with null.

MSDN说从 const 的角度来看,这是有道理的:

MSDN says this, which I understand and it makes sense from const perspective:


一个常量expression是可以在
编译时完全评估的表达式。因此,
引用类型的常量的唯一可能值是字符串和空引用。

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 a null reference.

但是,我不没有看到为什么或为什么我们要使用 null 常量的原因。因此,为什么为什么首先可以将 const 定义为引用类型(字符串除外),前提是它只能设置为 null ,如果这是一个故意的决定(我相信是这样),那么我们在哪里可以使用带有空值的常量?

However, I don't see the reason why or where we would use null constant. So why in the first place that a reference type (other than string) can be defined with const if it can be only set to null and if it was a deliberate decision (which I believe it is) then where can we use constant with null values?

更新:

当我们想到一个答案时,请让我们与我们拥有这个,为什么不那样……上下文不同地思考。

When we think of an answer, please let's think differently than "We have this so why not that..." context.

推荐答案

来自 MSDN


当编译器在C#源代码中遇到常量标识符(例如,月份)时,它将替换文字直接将其值转换为它产生的中间语言(IL)代码。因为在运行时没有与常量相关联的变量地址,所以const字段不能通过引用传递,也不能在表达式中显示为l值。

when the compiler encounters a constant identifier in C# source code (for example, months), it substitutes the literal value directly into the intermediate language (IL) code that it produces. Because there is no variable address associated with a constant at run time, const fields cannot be passed by reference and cannot appear as an l-value in an expression.

因为引用类型(空值和特殊的字符串除外)必须在运行时构造时间,以上对于引用类型是不可能的。

Because reference types (other than null, and strings which are special) need to be constructed at run time, the above would not be possible for reference types.

对于引用类型,您可以获得的最接近的是静态只读

For reference types, the closest you can get is static readonly:

class Foo
{
    // This is not a good idea to expose a public non-pure field
    public static readonly StringBuilder BarBuilder = new StringBuilder();
    public Foo(){
    }
}

与const不同替换(在调用代码中),静态只读创建引用类型的单个共享实例,该实例的细微差异(如果程序集版本已更改)。

Unlike const substitution (in the calling code), static readonly creates a single shared instance of the reference type which has subtle differences if assembly versions are changed.

尽管不能重新分配引用(通常),但是不排除在 StringBuilder 上调用非纯方法(例如 Append 等)。这不同于 consts ,后者的值类型和字符串是不可变的(可以说应该是 永恒 )。

Although the reference cannot (normally) be reassigned, it doesn't preclude calling non-pure methods on the StringBuilder (like Append etc). This is unlike consts, where value types and strings are immutable (and arguably should be "eternal").

这篇关于如果只给它们分配null,为什么允许将const与引用类型一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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