Created
February 1, 2015 07:00
-
-
Save ufcpp/43dfc2bb8188f7634f03 to your computer and use it in GitHub Desktop.
immutable and readonly in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
{ | |
public int X { get; set; } | |
} | |
class B | |
{ | |
public readonly A A = new A(); | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
var b = new B(); | |
b.A = new A(); // エラー: A は readonly | |
b.A.X = 10; // OK : X は別に readonly じゃない | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment