2016-08-01から1ヶ月間の記事一覧

Conditional 属性の練習

using System; using System.Diagnostics; class Program { [Conditional("TEST")] static void Foo() { Console.WriteLine("FOO!"); } static void Main() { Console.WriteLine("BEGIN"); Foo(); Console.WriteLine("END"); } } 実行結果です。 $ mcs -defi…

Type からインスタンスを生成する

using System; class Foo { public void Say() { Console.WriteLine("Foo!"); } } class Bar { public Bar(int n, string s) { Console.WriteLine("n:{0} s:{1}", n, s); } public void Say() { Console.WriteLine("Bar!"); } } class Program { static void…

SEND + MORE = MONEY

using System; using System.Collections.Generic; using System.Linq; class Program { static int ToInt(string s, int[] map) { int x = 0; foreach (var c in s) { x = 10 * x + map[c]; if (x == 0 && s.Length > 1) return -1; // leading zero } retu…

文字列の配列を文字のシーケンスに変換する

using System; using System.Linq; class Program { static void Main() { var ss = new[] { "foo", "bar", "baz" }; // 文字列の配列を文字のシーケンスにする var cs = ss.SelectMany(e => e); Console.WriteLine(string.Join(" ", cs)); // => f o o b a …

Frequencies メソッドを作る

シーケンスに含まれる要素の出現回数を求める Frequencies メソッドを作ってみました。 using System; using System.Collections.Generic; using System.Linq; static class Ext { public static Dictionary<T, int> Frequencies<T>(this IEnumerable<T> xs) { var d = new</t></t></t,>…

CompositeDisposable クラスの Dispose メソッドを呼び出した後に Add したときの振る舞い

CompositeDisposable クラスの Dispose メソッドを呼び出すと、CompositeDisposable に格納されている全ての要素に対して Dispose メソッドが呼ばれます。 CompositeDisposable クラスの Dispose メソッドを呼び出すと、IsDisposed プロパティは true となり…