Please try to find the output of following code without running it.
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
DoSomething("a", "b");
Console.ReadLine();
}
public static void DoSomething<T>(IList<T> set)
{
Console.WriteLine(set.Count);
}
public static void DoSomething<T>(params T[] items)
{
List<T> set = new List<T>();
foreach (T t in items)
{
if (t == null)
continue;
set.Add(t);
}
DoSomething(set);
}
}
Answer - it will go in infinite loop and you will get out of stack error. Reason behind this is that CLR thinks converting a List to T[] is easier then converting it to IList.
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
DoSomething("a", "b");
Console.ReadLine();
}
public static void DoSomething<T>(IList<T> set)
{
Console.WriteLine(set.Count);
}
public static void DoSomething<T>(params T[] items)
{
List<T> set = new List<T>();
foreach (T t in items)
{
if (t == null)
continue;
set.Add(t);
}
DoSomething(set);
}
}
Answer - it will go in infinite loop and you will get out of stack error. Reason behind this is that CLR thinks converting a List to T[] is easier then converting it to IList.
Comments
Post a Comment