readonly(C# 参考)readonly (C# Reference)

本文内容

readonly 关键字是一个可在字段上使用的修饰符。The readonly keyword is a modifier that you can use on fields. 当字段声明包括 readonly 修饰符时,该声明引入的字段赋值只能作为声明的一部分出现,或者出现在同一类的构造函数中。When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

示例Example

在此示例中,即使在类构造函数中给字段 year 赋了值,它的值仍无法在 ChangeYear 方法中更改:In this example, the value of the field year cannot be changed in the method ChangeYear, even though it is assigned a value in the class constructor:

class Age
{
    readonly int _year;
    Age(int year)
    {
        _year = year;
    }
    void ChangeYear()
    {
        //_year = 1967; // Compile error if uncommented.
    }
}

只能在下列上下文中对 readonly 字段进行赋值:You can assign a value to a readonly field only in the following contexts:

  • 在声明中初始化变量时,例如:When the variable is initialized in the declaration, for example:

    public readonly int y = 5;  
    
  • 对于实例字段,在包含字段声明的类的实例构造函数中;或者,对于静态字段,在包含字段声明的类的静态构造函数中。For an instance field, in the instance constructors of the class that contains the field declaration, or for a static field, in the static constructor of the class that contains the field declaration. 也只有在这些上下文中,将 readonly 字段作为 outref 参数传递才有效。These are also the only contexts in which it is valid to pass a readonly field as an out or ref parameter.

备注

readonly 关键字不同于 const 关键字。The readonly keyword is different from the const keyword. const 字段只能在该字段的声明中初始化。A const field can only be initialized at the declaration of the field.

 

      `readonly` 字段可以在声明或构造函数中初始化。</span><span class="sxs-lookup"><span data-stu-id="99a4f-113">A `readonly` field can be initialized either at the declaration or in a constructor.</span></span> <span data-ttu-id="99a4f-114">因此,根据所使用的构造函数,`readonly` 字段可能具有不同的值。</span><span class="sxs-lookup"><span data-stu-id="99a4f-114">Therefore, `readonly` fields can have different values depending on the constructor used.</span></span> <span data-ttu-id="99a4f-115">另外,虽然 `const` 字段是编译时常量,但 `readonly` 字段可用于运行时常量,如下面的示例所示:</span><span class="sxs-lookup"><span data-stu-id="99a4f-115">Also, while a `const` field is a compile-time constant, the `readonly` field can be used for runtime constants as in the following example:</span></span>  
public static readonly uint timeStamp = (uint)DateTime.Now.Ticks;  

示例Example

public class ReadOnlyTest
{
   class SampleClass
   {
      public int x;
      // Initialize a readonly field
      public readonly int y = 25;
      public readonly int z;

      public SampleClass()
      {
         // Initialize a readonly instance field
         z = 24;
      }

      public SampleClass(int p1, int p2, int p3)
      {
         x = p1;
         y = p2;
         z = p3;
      }
   }

   static void Main()
   {
      SampleClass p1 = new SampleClass(11, 21, 32);   // OK
      Console.WriteLine("p1: x={0}, y={1}, z={2}", p1.x, p1.y, p1.z);
      SampleClass p2 = new SampleClass();
      p2.x = 55;   // OK
      Console.WriteLine("p2: x={0}, y={1}, z={2}", p2.x, p2.y, p2.z);
   }
}
/*
 Output:
    p1: x=11, y=21, z=32
    p2: x=55, y=25, z=24
*/

在前面的示例中,如果使用如下的语句:In the preceding example, if you use a statement like this:

p2.y = 66; // Error

将收到编译器错误消息:you will get the compiler error message:

The left-hand side of an assignment must be an l-value

这与尝试给常数赋值时收到的错误相同。which is the same error you get when you attempt to assign a value to a constant.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值