Skip to content

Instantly share code, notes, and snippets.

@zs40x
Last active April 3, 2016 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zs40x/22a6c8015038f38e1adabaaaeaa6f68a to your computer and use it in GitHub Desktop.
Save zs40x/22a6c8015038f38e1adabaaaeaa6f68a to your computer and use it in GitHub Desktop.
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