. .
.NET
.NET , . , IComparable , . IEnumerable IEnumerator foreach, ICloneable .
. , foreach , Array IEnumerable IEnumerator. , , .
( IComparable)
IComparable CompareTo(), :
interface IComparable
{
int CompareTo(object obj)
}
:
o 0,
o ,
o ,
IComparable Monster
namespace ConsoleApplication1
{
// ,
class Monster: IComparable
{
public Monster(int , int , string )
{
this. = ;
this. = ;
this. = ;
}
public void Passport()
{
Console.WriteLine(" {0} \t = {1} = {2}", , , );
}
// , ..
public int CompareTo(object obj)
{
Monster temp = (Monster)obj;
if (this. > temp.) return 1;
if (this. < temp.) return -1;
return 0;
}
string ;
int , ;
}
class Program
{
static void Main(string[] args)
{
const int n = 3;
Monster[] mas = new Monster[n]; //
// ( )
mas[0] = new Monster(50, 50, "");
mas[1] = new Monster(100, 100, "");
mas[2] = new Monster(40, 10, "");
// Monster , // CompareTo()
Array.Sort(mas);
//
foreach (Monster x in mas)
x.Passport();
Console.ReadLine(); } } }
. Monster.
, .
. IComparer.
|
|
( IComparer)
System.Collections. Compare(), , .
interface IComparer
{
int Compare(object obj1,object obj2)
}
, , . .
Monster ( , SortBy) ( , SortBy). , Monster.