1.处理值类型时后台的一些常见操作:boxing(封箱)和unboxing(拆箱)
封箱是把值类型转换为System.Object类型,或者转换为由值类型实现的接口类型。拆箱反之。
封箱是隐式的。拆箱需要进行显示转换。
封箱允许在项的类型是object的集合中使用值类型。其次在一个区内部机制允许在值类型上调用object。
因此封箱与拆箱机制允许任何值类型,引用类型和object类型之间进行转换。
2.is 运算符
可以检查对象是否是给定类型,或者是否可以转换为给定类型。
<operand> is <type> (type 可以是类类型、接口类型或值类型等)
3. IComparable和IComparer
IComparable: 在要比较的对象的类中实现。 实现方法: CompareTo(object obj)
System.Collections.IComparer在一个单独的类中实现。 实现方法:Compare(object obj1, object obj2)
对于自己的类排序,必须在类定义中实现IComparable,或者创建一个支持IComparer的类来进行比较。
class Person : IComparable
{
public string Name;
public int Age;
public Person(string _name, int _age)
{
Name = _name;
Age = _age;
}
public int CompareTo(object obj)
{
if (obj is Person)
{
Person otherPerson = obj as Person;
Console.WriteLine(this.Age + " . other: " + otherPerson.Age + " , " + (this.Age - otherPerson.Age));
return this.Age - otherPerson.Age;
}
else
{
throw new ArgumentException("Object to compare to is not a person object");
}
}
}
public class PersonCompareName : IComparer
{
public static IComparer Default = new PersonCompareName();
public int Compare(object x, object y)
{
if (x is Person && y is Person)
{
Console.WriteLine(((Person)x).Name + " , other : " + ((Person)y).Name + " , " +
Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name));
return Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name);
}
else
{
throw new ArgumentException("One or both objects to compare are not Person objects");
}
}
}
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person("Jim", 32));
list.Add(new Person("Bob", 23));
list.Add(new Person("Bert", 45));
list.Add(new Person("Ernie", 54));
Console.WriteLine("Unsorted people: ");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
Console.WriteLine("People sorted with default comparer by age: ");
list.Sort();
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
Console.WriteLine("People sorted with nondefault comparer by name: ");
list.Sort(PersonCompareName.Default);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.ReadKey();
}
4.as 运算符: 一般适用于引用类型和装箱的转换。
explicit与implicit 转换运算符;
封箱是把值类型转换为System.Object类型,或者转换为由值类型实现的接口类型。拆箱反之。
封箱是隐式的。拆箱需要进行显示转换。
封箱允许在项的类型是object的集合中使用值类型。其次在一个区内部机制允许在值类型上调用object。
因此封箱与拆箱机制允许任何值类型,引用类型和object类型之间进行转换。
2.is 运算符
可以检查对象是否是给定类型,或者是否可以转换为给定类型。
<operand> is <type> (type 可以是类类型、接口类型或值类型等)
3. IComparable和IComparer
IComparable: 在要比较的对象的类中实现。 实现方法: CompareTo(object obj)
System.Collections.IComparer在一个单独的类中实现。 实现方法:Compare(object obj1, object obj2)
对于自己的类排序,必须在类定义中实现IComparable,或者创建一个支持IComparer的类来进行比较。
class Person : IComparable
{
public string Name;
public int Age;
public Person(string _name, int _age)
{
Name = _name;
Age = _age;
}
public int CompareTo(object obj)
{
if (obj is Person)
{
Person otherPerson = obj as Person;
Console.WriteLine(this.Age + " . other: " + otherPerson.Age + " , " + (this.Age - otherPerson.Age));
return this.Age - otherPerson.Age;
}
else
{
throw new ArgumentException("Object to compare to is not a person object");
}
}
}
public class PersonCompareName : IComparer
{
public static IComparer Default = new PersonCompareName();
public int Compare(object x, object y)
{
if (x is Person && y is Person)
{
Console.WriteLine(((Person)x).Name + " , other : " + ((Person)y).Name + " , " +
Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name));
return Comparer.Default.Compare(((Person)x).Name, ((Person)y).Name);
}
else
{
throw new ArgumentException("One or both objects to compare are not Person objects");
}
}
}
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
list.Add(new Person("Jim", 32));
list.Add(new Person("Bob", 23));
list.Add(new Person("Bert", 45));
list.Add(new Person("Ernie", 54));
Console.WriteLine("Unsorted people: ");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
Console.WriteLine("People sorted with default comparer by age: ");
list.Sort();
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.WriteLine();
Console.WriteLine("People sorted with nondefault comparer by name: ");
list.Sort(PersonCompareName.Default);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("{0} ({1})", (list[i] as Person).Name, (list[i] as Person).Age);
}
Console.ReadKey();
}
4.as 运算符: 一般适用于引用类型和装箱的转换。
explicit与implicit 转换运算符;