Last active
September 14, 2017 20:53
-
-
Save ChrisMissal/320122fb8cc9411b4fec5691220a162b to your computer and use it in GitHub Desktop.
Auto property initializers vs Expression-bodied members. Explained here: http://thebillwagner.com/Blog/Item/2015-07-16-AC6gotchaInitializationvsExpressionBodiedMembers
This file contains hidden or 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 Test | |
{ | |
public class Name | |
{ | |
public string Text { get; set; } | |
} | |
public abstract class Widget | |
{ | |
public abstract Name Name { get; } | |
} | |
public class Cog : Widget | |
{ | |
public override Name Name => new Name { Text = "Cog" }; | |
} | |
[Fact] | |
public void what_the_heck() | |
{ | |
Widget widget = new Cog(); | |
widget.Name.Text = "New Cog"; | |
Assert.Equal(widget.Name.Text, "New Cog"); | |
} | |
} |
This file contains hidden or 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 Test | |
{ | |
public class Name | |
{ | |
public string Text { get; set; } | |
} | |
public abstract class Widget | |
{ | |
public abstract Name Name { get; } | |
} | |
public class Cog : Widget | |
{ | |
public override Name Name { get; } = new Name { Text = "Cog" }; | |
} | |
[Fact] | |
public void what_the_heck() | |
{ | |
Widget widget = new Cog(); | |
widget.Name.Text = "New Cog"; | |
Assert.Equal(widget.Name.Text, "New Cog"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The first is supposed to fail ... Everytime you access the
Name
variable in theCog
class, it returns a new instance ofName
.Even if
Text
is set, it would revert to default whenName
is accessed again, making theAssert.Equal
fail