noimage

TechQuiz Question 3: Nullable boxing

Why does Foo<int?> throw an exception? 1: static void Foo<T>() where T : new() 2: { 3: T t = new T(); 4: Console.WriteLine(“ToString: ” + t.ToString()); 5: Console.WriteLine(“GetHashCode: ” + t.GetHashCode()); 6: Console.WriteLine(“Equals: ” + t.Equals(t)); 7: Console.WriteLine(“GetType: ” + t.GetType()); 8: } 9:   10: public void Main() 11: { 12: Foo<int>(); 13: Foo<int?>(); // Exception thrown 14:...

noimage

TechQuiz Question 1 Why doesn’t my code throw an exception?

Inside VincisAqtion we’ve started with a little TechQuiz. We’re planning on  creating weekly questions to publish them on our intranet and next to the coffee machine. The first question is: Why doesn’t the following code throw an exception? static IEnumerable<char> CapitalLetters(string input){    if (input == null)    {        throw new ArgumentNullException(input);    }    foreach (char c in input)    {        yield return char.ToUpper(c);    }}          static void Main()         {             CapitalLetters(null);         } The code...