「マイクロソフト系技術情報 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。
2000年から開発が始まったマイクロソフト(アンダース・ヘルスバーグ率いるチーム)が設計・開発したプログラミング言語。
マルチパラダイム
C++, Delphi, Eiffel, Java, LISP
D言語, F#, Java, Nemerle, Vala
Windows, macOS, Linuxなど
Apacheライセンス (Roslyn)
忘れ易いスニペット置き場が欲しかったので。
型推論を利用したローカル変数の宣言。
一時的に使用される型を簡単に定義する。
var her = new { Name = "Jane Doe", Age = 20 } var him = new { Name = "John Doe", Age = 20 }
値型を参照型として扱う。
// ref戻り値をref変数で受け取る ref int max = ref Max(ref x, ref y); // limitとmaxは同じ値を参照する ref int limit = ref max;
開発ツールなどが裏で使用していることが多い。
(ユーザー・コーディング部分と、designer生成部分)
class MyClass { int a; int b; }
↓↓↓
partial class MyClass { int a; } partial class MyClass { int b; }
メンバ変数+プロパティ・プロシージャの略記が可能。
private int __value; public int Value { get { return __value; } private set { __value = value; } }
↓↓↓
public int Value { get; private set; }
public int MethodB(int A = 0, int B = 0, int C = 0) { return A + B + C; }
ref変数で受け取る場合、
ref戻り値として返すなどする。
上記のオプション引数を選択的に指定できる。
public void MethodA() { // 第1引数と第2引数を指定、第3引数は未指定: Console.WriteLine("Ans: " + MethodB(1, 2)); // Ans: 3 … 1 + 2 + 0となっている // 第1引数と第3引数を指定、第2引数は未指定: Console.WriteLine("Ans: " + MethodB(A: 1, C: 3)); // Ans: 4 … 1 + 0 + 3となっている }
クラスでも使用可能。STLっぽく書く(コンセプトは違うっポイ)。
匿名デリゲートに次ぐ「delegate」の拡張。
public IEnumerable<string> Read(string path, Func<string, string> fx) { var result = new List<string>(); using (var reader = new StreamReader(path)) { while (reader.Peek() >= 0) { var line = reader.ReadLine(); result.Add(fx(line)); } } return result; }
XXXX.Read("hoge.txt", s => s + ".txt");
上記のように「Action、Funcのデリゲートの型」を使用する。
public static class StringUtil { public static string Repeat(this string str, int count) { var array = new string[count]; for (var i = 0; i < count; ++i) array[i] = str; return string.Concat(array); } }
// 静的メソッドとしての呼び出し StringUtil.Repeat("foo", 4); // 拡張メソッド(インスタンス・メソッド)としての呼び出し "foo".Repeat(4);
int[] array1 = new int[] { 1, 3, 5, 7, 9 };
List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Dictionary<string, string> openWith = new Dictionary<string, string>() { {"txt", "notepad.exe"}, {"bmp", "paint.exe"}, {"dib", "paint.exe"}, {"rtf", "wordpad.exe"} };
var a = new[] {"foo", "bar", null};
Point p = new Point{ X = 0, Y = 1 };
var x = new Line { A = { X = 1, Y = 2 }, B = { X = 3, Y = 4 }, };
// anon is compiled as an anonymous type var anon = new { Name = "Terry", Age = 34 };
C#6から自動実装プロパティの初期化が可能。
class Class1 { public string Auth { get; set; } = "BlahBlah"; }
System.Type type = typeof(int);
int i = 0; System.Type type = i.GetType();
expression as type; expression is type ? (type)expression : (type)null;
C#では、break;を明記するものの、フォールスルーを禁止されている。
switch(変数) { case 値1: いくつかの文1 // 変数の値 == 値1 のとき実行される break; case 値2: いくつかの文2 // 変数の値 == 値2 のとき実行される break; ・ ・ ・ default: いくつかの文 // 変数の値がどの値とも異なるとき実行される break; }
void Decide(object obj) { switch (obj) { case int num when num < 0: Console.WriteLine($"{num}は負の数です。"); break; case int num: Console.WriteLine($"{num}を二乗すると{num * num}です。"); break; case "B": Console.WriteLine($"これはBです。"); break; case string str when str.StartsWith("H"): Console.WriteLine($"{str}はHから始まる文字列です。"); break; case string str: Console.WriteLine($"{str}は文字列です。"); break; case null: Console.WriteLine($"nullです"); break; default: Console.WriteLine("判別できませんでした"); break; } }
以下の例で、始めにaを評価してその評価値が、
int x = a ? b : c;
object obj1 = null; object obj2 = new object(); object obj3 = new object(); return obj1 ?? obj2 ?? obj3; // obj2 を返す
int? i = null; int j = i ?? -1; // nullをint型に代入することはできない