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
| using static System.Console; | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| Derived.X(0); // object | |
| Derived.X(""); // object。型よりも派生クラス側が優先 | |
| Base.X(""); // string | |
| } |
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
| struct X | |
| { | |
| X() {} | |
| }; | |
| void main() | |
| { | |
| X x(); // X 型の変数 x に対して、コンストラクター X() で初期化 | |
| } |
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
| delegate bool TryParse<T>(string text, out T result); | |
| class Program | |
| { | |
| static void Main() | |
| { | |
| TryParse<int> parse1 = (string s, out int result) => int.TryParse(s, out result); // これは OK | |
| TryParse<int> parse2 = (s, out int result) => int.TryParse(s, out result); // ダメ。1個だけ型名明示とかできない。 | |
| TryParse<int> parse3 = (s, out result) => int.TryParse(s, out result); // ダメ。out 型推論とかない。この機能がほしい。 | |
| } |
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 void Tally(IEnumerable<int> values, out int sum, out int count) { ... } | |
| int s, c; | |
| Tally(myValues, out s, out c); | |
| Console.WriteLine($"Sum: {s}, count: {c}"); |
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 Tuple<int, int> Tally(IEnumerable<int> values) { ... } | |
| var t = Tally(myValues); | |
| Console.WriteLine($"Sum: {t.Item1}, count: {t.Item2}"); |
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 struct TallyResult { public int Sum; public int Count; } | |
| public TallyResult Tally(IEnumerable<int> values) { ... } | |
| var t = Tally(myValues); | |
| Console.WriteLine($"Sum: {t.Sum}, count: {t.Count}"); |
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 (int sum, int count) Tally(IEnumerable<int> values) { ... } | |
| var t = Tally(myValues); | |
| Console.WriteLine($"Sum: {t.sum}, count: {t.count}"); |
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 async Task<(int sum, int count)> TallyAsync(IEnumerable<int> values) { ... } | |
| var t = await TallyAsync(myValues); | |
| Console.WriteLine($"Sum: {t.sum}, count: {t.count}"); |
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
| (int sum, int count) result = (1, 2); // 左辺の型を見て | |
| var result = (sum: 1, count: 2); // 名前付き引数の要領で、右辺から型推論 |
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
| var t = (sum: 1, count: 2); // タプル構築 | |
| (var sum, var count) = t; // タプル分解 | |
| (sum, count) = t; // 既存変数へのタプル分解(これを認めるかどうかは未定) |