Const and readonly usage Summary

Source: Internet
Author: User

In the past, const was used to define constants during development, and readonly was rarely used, because it always felt that the use of const was sufficient. In the past two daysCodeAgain, we can see the use of readonly, and it feels awkward. If a constant is defined in const, it cannot be changed again during use. Otherwise, the value is assigned again in the constructor. Due to the confusion about readonly usage, const and readonly are summarized based on the learning attitude for future reference:

Name

Static constants (Const)

Dynamic constant/read-only variable (readonly)

Scope of use

Global and local

Global

Initial Value

The initial value must be assigned during definition.

No value is assigned during definition.

Assignment Method

Value assignment during definition

Assign values when defining and assign values in Constructors

Access Method

Type access

Instance Object Access

Static

Cannot be used together with static

It can be used together with static. after use, if you want to assign an initial value to the constructor, you must use a static non-argument constructor.

Application Type

Only the value type and string type can be applied.

Any Type

Instance (vs2008)

 1   Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5
6 Namespace Constandreadonly
7 {
8 Class Program
9 {
10 Static Void Main ( String [] ARGs)
11 {
12 Console. writeline (consttest. Str ); // Access by type
13 Readonlytest RT = New Readonlytest ( " 123456 " );// Access through instance objects and assign values through Constructors
14 Console. writeline (RT. Str );
15 Console. writeline (RT. strnull );
16 String [] Str1 = { " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " };
17 Readonlytest rt2 = New Readonlytest (str1 ); // Assign an array through the constructor
18 Console. writeline (rt2.str1. Count ());
19 Console. Readline ();
20 }
21 }
22 Class Consttest
23 {
24 Public Const String STR =" Const Test " ; // Unchangeable
25 }
26 Class Readonlytest
27 {
28 Public Readonly String STR = " Readonly Test " ;
29 Public Readonly String Strnull; // No initial value is assigned. The default value is null;
30 Public Readonly String [] Str1;
31 Public Readonlytest (String S)
32 {
33 This . Strnull = This . STR;
34 This . Str = s;
35 }
36 Public Readonlytest ( String [] S)
37 {
38 This . Str1 = s;
39 }
40 }
41 }

 

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.