List.Sort in C#: comparer being called with null object

后端 未结 7 1192
攒了一身酷
攒了一身酷 2021-01-01 11:48

I am getting strange behaviour using the built-in C# List.Sort function with a custom comparer.

For some reason it sometimes calls the comparer class\'s Compare meth

7条回答
  •  無奈伤痛
    2021-01-01 12:35

    I stumbled across this issue myself, and found that it was related to a NaN property in my input. Here's a minimal test case that should produce the exception:

    public class C {
    
        double v;
    
        public static void Main() {
            var test =
                new List { new C { v = 0d },
                              new C { v = Double.NaN },
                              new C { v = 1d } };
            test.Sort((d1, d2) => (int)(d1.v - d2.v));
        }
    
    }
    

提交回复
热议问题