Anonymous IComparer implementation

前端 未结 8 954
一整个雨季
一整个雨季 2021-02-03 17:57

Is it possible to define an anonymous implementation of IComparer?

I believe Java allows anonymous classes to be defined inline - does C#?

Looking at this code I

8条回答
  •  隐瞒了意图╮
    2021-02-03 18:12

    Even though you can't create anonymous classes that implement interfaces, you can usually use the Comparison Delegate instead of the IComparer Interface in most cases (like sorting, etc.):

    Array.Sort(arr, (x, y) => 1);
    

    Also there are some built-in implementations of IComparer like the Comparer Class or the StringComparer Class...

提交回复
热议问题