class类型数组值比较:
继承IEquatable<>:
//file1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PC
{
class Person : IEquatable<Person>
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return "shihuamarryme";
}
public override bool Equals(Object obj)
{
Console.WriteLine("override--------------------->");
if (obj == null)
{
return base.Equals(obj);
}
return this.Equals(obj as Person);
}
public bool Equals(Person other)
{
Console.WriteLine("not override--------------------->");
if (other == null)
{
return base.Equals(other);
}
return (this.Id == other.Id && this.FirstName == other.FirstName && this.LastName == other.LastName);
}
//public override int GetHashCode()
//{
// return this.Id.GetHashCode();
//}
}
}
//mian.cs
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using PC;
namespace test
{
class test
{
public static void Main(string[] args)
{
Person p1 = new Person { FirstName = "shihua", LastName = "MarryMe" };
new Person { FirstName = "shihua", LastName = "MarryMe" };
Person[] ps1 = { new Person { FirstName = "shihua", LastName = "MarryMe" },
new Person { FirstName = "shihua", LastName = "MarryMe" },
p1 };
Person[] ps2 = { new Person { FirstName = "shihua", LastName = "MarryMe" },
new Person { FirstName = "shihua", LastName = "MarryMe" },
p1 };
if ((ps1 as IStructuralEquatable).Equals(ps2, EqualityComparer<Person>.Default))
{
Console.WriteLine("is same");
}
return;
}
}
}
class类型数组排序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
public class Demo : IComparable<Demo>
{
private string firstName_;
public string FirstName
{
get
{
return this.firstName_;
}
set
{
this.firstName_ = value;
}
}
public int CompareTo(Demo other)
{
if (other == null)
{
return 1;
}
int result = System.String.Compare(this.FirstName, other.FirstName);
return result;
}
}
class test
{
public static void Main(string[] args)
{
Demo[] array = { new Demo { FirstName = "x"} ,
new Demo {FirstName = "a"},
new Demo {FirstName = "c" },
new Demo {FirstName = "b" } };
Array.Sort(array);
foreach(var val in array)
{
Console.WriteLine(val.FirstName);
}
return;
}
}
}
class类型数组排序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
public class Demo /*: IComparable<Demo>*/
{
private string firstName_;
public string FirstName
{
get
{
return this.firstName_;
}
set
{
this.firstName_ = value;
}
}
public int CompareTo(Demo other)
{
if (other == null)
{
return 1;
}
int result = System.String.Compare(this.FirstName, other.FirstName);
return result;
}
}
class CompareDemo : IComparer<Demo>
{
//private CompareDemo CompareType;
//public CompareDemo(CompareDemo compareType)
//{
// this.CompareType = compareType;
//}
public int Compare(Demo lh, Demo rh)
{
if(lh == null && rh == null)
{
return 0;
}
if(lh == null && rh != null)
{
return 0;
}
if(lh != null && rh == null)
{
return 0;
}
int result = System.String.Compare(lh.FirstName, rh.FirstName);
return result;
}
}
class test
{
public static void Main(string[] args)
{
Demo[] array = { new Demo { FirstName = "x"} ,
new Demo {FirstName = "a"},
new Demo {FirstName = "c" },
new Demo {FirstName = "b" } };
Array.Sort(array, new CompareDemo());
foreach (var val in array)
{
Console.WriteLine(val.FirstName);
}
return;
}
}
}