ascendingcomparer.cs
来自「Microsoft?Visual C#?.NET (Core Reference」· CS 代码 · 共 37 行
CS
37 行
using System;
using System.Collections;
namespace MSPress.CSharpCoreRef.ch08Comparer
{
/// <summary>
/// Summary description for AscendingComparer.
/// </summary>
public class AscendingComparer: IComparer
{
public int Compare(object x, object y)
{
// Simplified version using built-in Comparer class:
// return Comparer.Default.Compare(x, y);
int result = 0;
if(x == null && y == null)
result = 0;
else if(x == null)
result = -1;
else if(y == null)
result = 1;
else
{
if(x.GetType() != y.GetType())
throw new ArgumentException("Invalid comparison");
IComparable comp = x as IComparable;
if(comp == null)
throw new ArgumentException("Invalid comparison");
result = comp.CompareTo(y);
}
return result;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?