C # programming Study #2

Source: Internet
Author: User

  • ReadOnly (C # Reference)
    • 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
    • Class Age    {        readonly int _year;        Age (Int.)        {            _year = year;        }        void Changeyear ()        {            //_year = 1967;//Compile error if uncommented.        }    }

    • The ReadOnly keyword differs from the const keyword.
    • const field can only is initialized at the declaration of the field. " > const   field can only be initialized in the declaration of that field.
    • readonly field can be initialized either at the declaration or in a constructor." The readonly   field can be initialized in a declaration or constructor. readonly fields can has different values depending on the constructor used. " Therefore, depending on the constructor used, the readonly field may have different values. const field is a Compile-time constant, the readonly field can be used to runtime constants as in the following example: "> Additionally, const field is a compile-time constant , and the readonly field can be used to run a constant number of times

C # programming Study #2

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.