-
-
Save ufcpp/54a1346667e4c418706fa3bbe4ff025e to your computer and use it in GitHub Desktop.
Records with sealed base ToString override #4174
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
// この状態(明示的な ToString オーバーライドなし)は、record のデフォルト動作。 | |
// | |
// R { Name = abc } | |
// D1 { Name = D1 } | |
// D2 { Name = D2 } | |
Console.WriteLine(new R("abc")); | |
Console.WriteLine(new D1()); | |
Console.WriteLine(new D2()); | |
record R(string Name); | |
record D1() : R(nameof(D1)); | |
record D2() : R(nameof(D2)); |
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
// 基底クラスだけ ToString オーバーライド。 | |
// 基底クラスで作った処理が、派生クラス側では record のデフォルト動作で再度オーバーライドされちゃう。 | |
// | |
// name is abc | |
// D1 { Name = D1 } | |
// D2 { Name = D2 } | |
Console.WriteLine(new R("abc")); | |
Console.WriteLine(new D1()); | |
Console.WriteLine(new D2()); | |
record R(string Name) | |
{ | |
public override string ToString() => "name is " + Name; | |
} | |
record D1() : R(nameof(D1)); | |
record D2() : R(nameof(D2)); |
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
// 基底クラスだけ ToString オーバーライド。 | |
// さっきの状態に「sealed」が付いただけ。 | |
// これで、派生クラス側の ToString 自動生成を抑止できる。 | |
// | |
// name is abc | |
// name is D1 | |
// name is D2 | |
Console.WriteLine(new R("abc")); | |
Console.WriteLine(new D1()); | |
Console.WriteLine(new D2()); | |
record R(string Name) | |
{ | |
public sealed override string ToString() => "name is " + Name; | |
} | |
record D1() : R(nameof(D1)); | |
record D2() : R(nameof(D2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment