.NET 4.x Приведение Dictionary возвращает null - C#
Формулировка задачи:
Объясните пожалуйста почему Con возвращает null ? не могу понять(
class B
{
public B(int b)
{
_b = b;
}
public int _b;
}
class Program
{
static void Main(string[] args)
{
var dA = new Dictionary<string, B>();
for(var i = 0; i < 10; i++)
dA.Add(i.ToString(), new B(i));
var dB = Con<B>(dA.ToDictionary(x => x.Key, x => (object)x));
}
private static Dictionary<string, T> Con<T>(Dictionary<string, object> dObj)
{
return dObj as Dictionary<string, T>;
}
}Решение задачи: «.NET 4.x Приведение Dictionary возвращает null»
textual
Листинг программы
var dA = new Dictionary<string, B>(); for(var i = 0; i < 10; i++) dA.Add(i.ToString(), new B(i)); // Cannot implicitly convert type 'System.Collections.Generic.Dictionary<string,B>' // to 'System.Collections.Generic.Dictionary<string,object>' Dictionary<string,object> dObj = dA;