ReadOnly (C # Reference)

Source: Internet
Author: User

Visual Studio 2015

Description

For the latest documentation on C #, visit the C # Guide on docs.microsoft.com.

The ReadOnly keyword is a modifier that can be used on a field. When a field declaration includes a readonly modifier, the field assignment introduced by the Declaration can only occur as part of the declaration or in a constructor of the same class.

Example

In this example, the value of the field year cannot be changed in the Changeyear method, even if it is assigned a value in the class constructor.

C#

1     class Age2     {3         ReadOnly int_year;4Age (intYear )5         {6_year =Year ;7         }8         voidchangeyear ()9         {Ten             //_year = 1967;//Compile Error if uncommented. One         } A}

The ReadOnly field can only be assigned in the following contexts:

    • When you initialize a variable in a declaration, for example:
      public readonly int y = 5;
    • For instance fields, in the instance constructor 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. It is also only in these contexts that the ReadOnly field is passed as an out or ref parameter to be valid.

Description

The ReadOnly keyword differs from the const keyword. The const field can only be initialized in the declaration of the field. The readonly field can be initialized in a declaration or constructor. Therefore, depending on the constructor used, the ReadOnly field may have different values. In addition, the Const field is a compile-time constant, and the ReadOnly field can be used to run a constant number, as shown in the following example:

public static readonly UINT TIMESTAMP = (UINT) DateTime.Now.Ticks;

Example

C#

1      Public classreadonlytest2     {3        classSampleClass4        {5            Public intx;6           //Initialize a readonly field7            Public ReadOnly inty = -;8            Public ReadOnly intZ;9  PublicSampleClass ()Ten           { One              //Initialize a readonly instance field Az = -; -           } -  PublicSampleClass (intP1,intP2,intp3) the           { -x =P1; -y =P2; -z =P3; +           } -        } + Static voidMain () A        { atSampleClass P1 =NewSampleClass ( One, +, +);//OK -Console.WriteLine ("p1:x={0}, Y={1}, z={2}", p1.x, P1.y, p1.z); -SampleClass P2 =NewSampleClass (); -p2.x = -;//OK -Console.WriteLine ("p2:x={0}, Y={1}, z={2}", p2.x, P2.y, p2.z); -        } in     } -     /* to Output: + p1:x=11, y=21, z=32 - p2:x=55, y=25, z=24 the     */

In the previous example, if you use such a statement:

P2.Y = 66;//Error

You receive a compiler error message:

Theleft-hand side of an assignment must is an l-value

This is the same error that you receive when you try to assign a value to a constant.

From

ReadOnly (C # Reference)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.