readonly keyword-modified variables can be modified, but cannot be reassigned

Source: Internet
Author: User

The official MSDN explanation

ReadOnlyA keyword is a modifier that you can use on a field. When a field declaration includes ReadOnlymodifier, the field assignment introduced by the Declaration can only occur as part of the declaration or in a constructor of the same class. Many beginners read the book will think, ReadOnly modified variables in the future can not be modified, in the future development of the readonly variables do not modify the operation, the formation of thinking stereotypes, this concept is wrong.

The first thing to make clear: change! = Redistribution (Assignment)

For a simple type (such as int), the change is equal to the re-assignment, because the default operator is only =, but not necessarily for complex types.

For example:

For class type, modify the value of its field property.

For collection types, add, remove, and clear content.

We often find the following similar usage of readonly in Microsoft code:

1 public interface IA {}
2 public class A1:ia {}
3 public class A2:ia {}
4
5 public static Class Acontainer
6 {
7 private static readonly dictionary<string, ia> Items = new dictionary<string, ia> ();
8 public static dictionary<string, ia> as{get {return Items;}}
9}

The Acontainer can then be modified externally

1 class Program
2 {
3 static void Main ()
4 {
5 Console.WriteLine (AContainer.As.Count);
6 AContainer.As.Add ("A1", New A1 ());
7 Console.WriteLine (AContainer.As.Count);
8 AContainer.As.Add ("A2", New A2 ());
9 Console.WriteLine (AContainer.As.Count);
//We can remove all the item of the collection
AContainer.As.Clear ();
Console.WriteLine (AContainer.As.Count);
Console.readkey ();
14}
15
16}

Output:

Conclusion:

You can modify the contents of a readonly variable in an external (non-declarative and constructor) operation.

One way to develop extensions in Microsoft samples and open source code is to use a static ReadOnly collection container class that includes the default implementation and then allows the user to add or replace in development.

such as the modelbinderproviders in MVC3 ,viewengines are similar implementations.

Of course we can also change the value of ReadOnly by unconventional means (reflection, manipulation of memory).

such as reflection

1 using System;
2 using System.Reflection;
3
4 namespace Sovt
5 {
6 public class Foo
7 {
8 private readonly int bar;
9 public Foo (int num) {bar = num;}
Ten public int Getbar () {return bar;}
11}
12
Class Program
14 {
static void Main ()
16 {
+ foo foo = new Foo (123);
Console.WriteLine (foo. Getbar ());
FieldInfo FieldInfo = typeof (Foo). GetField ("Bar", BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo! = null)
Fieldinfo.setvalue (foo, 567);
Console.WriteLine (foo. Getbar ());
Console.readkey ();
24}
25}
26}

Output

readonly keyword-modified variables can be modified, but cannot be reassigned

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.