「マイクロソフト系技術情報 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 }
開発ツールなどが裏で使用していることが多い。
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;
}
上記のオプション引数を選択的に指定できる。
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)
{上記のように「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";
}
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型に代入することはできない