C#の問題
C#の問題 2題
問題 その1 : 下記の実行結果は?
string s1 = null; string s2 = null; string s = s1 + s2; Console.WriteLine(s.Length);
選択肢
- 0と出力
- 3行目でエラー
- 4行目でエラー
- その他
問題 その2 : 下記の実行結果は?
class Program { static object o = new object(); static int Fac(int n) { lock (o) { Thread.Sleep(1000); if (n <= 1) return 1; return n * Fac(n - 1); } } static void Main(string[] args) { Thread th1 = new Thread(new ThreadStart(delegate { Console.WriteLine("th1 : Fac(2) = {0}", Fac(2)); })); Thread th2 = new Thread(new ThreadStart(delegate { Console.WriteLine("th2 : Fac(2) = {0}", Fac(2)); })); th1.Start(); th2.Start(); } }
選択肢
- 2秒後と4秒後にそれぞれ出力
- 2秒後に2行とも出力
- 何も出力されずに無限ループ
- その他
余談
あるときから、VisualStudioでFormのデザインを開くと落ちるようになった。原因は、タイマのハンドラでNullReferenceExceptionを起こしていたため。