我这里编译不过, Error 1 Operator '==' cannot be applied to operands of type 'System.Collections.Generic.KeyValuePair<int,int>' and '<null>' E:\TestPrj\vs2012\WindowsFormsApplication2\WindowsFormsApplication2\Form2.cs 31 31 WindowsFormsApplication2KeyValuePair<int,int>是Struct,因此不能还null比较。FirstOrDefault返回的是第一个或者Default, 引用类型default是NULL, 而比较KeyValuePair,你要这么写: var c = dic.Where(x => x.Key > 8).FirstOrDefault(); //Console.WriteLine(c == null); //报错 Console.WriteLine(c.Equals((default(KeyValuePair<int,int>))));
var c = dic.Where(x => x.Key > 8).First();这句报错,不是c==null报错
你应该用FirstOrDefault
Error 1 Operator '==' cannot be applied to operands of type 'System.Collections.Generic.KeyValuePair<int,int>' and '<null>' E:\TestPrj\vs2012\WindowsFormsApplication2\WindowsFormsApplication2\Form2.cs 31 31 WindowsFormsApplication2KeyValuePair<int,int>是Struct,因此不能还null比较。FirstOrDefault返回的是第一个或者Default, 引用类型default是NULL, 而比较KeyValuePair,你要这么写:
var c = dic.Where(x => x.Key > 8).FirstOrDefault();
//Console.WriteLine(c == null); //报错
Console.WriteLine(c.Equals((default(KeyValuePair<int,int>))));
...