Last active
April 3, 2016 19:39
-
-
Save zs40x/22a6c8015038f38e1adabaaaeaa6f68a to your computer and use it in GitHub Desktop.
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
public class Money | |
{ | |
public decimal Amount { get; private set; } = 20m; | |
public void doStuff() | |
{ | |
Amount *= 2; | |
} | |
} | |
public class ImmutableMoney | |
{ | |
public decimal Amount { get; } // no set accessor!; | |
public ImmutableMoney(decimal amount) | |
{ | |
Amount = amount; | |
} | |
public void doStuff() | |
{ | |
// error CS0200: Property (...)) cannot be assigned to -- it is read only | |
// Amount *= 2; <- impossible | |
} | |
} | |
public class Program | |
{ | |
static void Main() | |
{ | |
var money = new Money(); | |
money.doStuff(); | |
Console.WriteLine(money.Amount); | |
var immutable = new ImmutableMoney(10m); | |
Console.WriteLine(immutable.Amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment