Object类
1.简介
Object类是 .NET Framework 中所有类的最终基类;Object 是 System.Object 类的别名。所以对象(Object)类型可以被分配任何其他类型(值类型、引用类型、预定义类型或用户自定义类型)的值。但是,在分配值之前,需要先进行类型转换。
小object是大Object的一个别名(C#),就如小int是Int32的别名一样。
当一个值类型转换为对象类型时,则被称为 装箱;另一方面,当一个对象类型转换为值类型时,则被称为 拆箱。
object obj;
obj = 100; // 这是装箱
int nval = (int)obj;//这是拆箱
2.Object公开的实例方法
Equals、ToString、GetHashCode这三个方法是虚方法,可重写。GetType是外部方法.
-
Equals
1、静态Equals方法
public static bool Equals(Object objA, Object objB);
保证Equals在未重定义时也可使用2、实例Equals方法(可重写)
public virtual bool Equals(Object obj);
如果两个对象具有相同的值,就返回true,详情请参考C# 对象相等性判断和同一性判断
-
GetHashCode方法
返回对象的值的哈希值,详情请参考C# 对象哈希码
-
ToString方法
未重定义时,返回类型的完整名称(this.GetType().FullName)。例如,核心类型(如Boolean和Int32)类型重写该方法来返回他们的值的字符串表示.
如namespace test内的Point类实例p1,则p1.ToString
输出为test.Point
(重载其实就是实现类内的指定字符串输出)
-
GetType方法
返回从一个Type派生的一个类型的实例,指出调用GetType的那个对象是什么类型,返回的Type对象和反射类配合,获取与对象的类型有关的元数据信息.GetType是非虚方法,目的是防止类重写该方法,隐瞒起类型,破坏类型的安全性.
如namespace test内的Point类实例p1,则
p1.GetType
输出为test.Point
3.Object受保护的方法
-
MemberwiseClone方法
这个非虚方法创建类型的新实例,并将新对象的实例字段设于this对象的实例字段完全一致,返回对新实例的引用
(相当于创造一个与当前this指向对象一毛一样的新对象)
#region object类的MemberwiseClone应用举例
public class Program
{
static void Main(string[] args)
{
var p = new Person
{
Name = "张三",
Age = 11
};
var t=p.Clone();
Console.WriteLine("Origin object:{0},{1}", p.Name, p.Age);
Console.WriteLine("Clone object:{0},{1}", t.Name, t.Age);
Console.ReadKey();
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person Clone()
{
return (Person)MemberwiseClone();
}
}
#endregion
注:加#region和#endregion的作用主要是 :可以将这块区域的代码折叠起来,便于查看。
最后输出是:
-
Finalize方法
在垃圾回收器判断对象应该作为垃圾被回收之后,在对象的内存实际被回收之前,会调用这个虚方法.需要在回收内存前执行清理工作的类型应该重写该方法.
4.Object应用实例
C# Object 类的详细解释及应用 举例
program.cs:
#region object类的应用举例。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace test
{
class Program
{
static void Main(string[] args)
{
// Construct a Point object.
Point p1 = new Point(1, 2);
//创建同类型新实例
Point p2 = p1.Copy();
// Make another variable that references the first Point object.
Point p3 = p1;
// The line below displays false because p1 and p2 refer to two different objects.
Console.WriteLine(Object.ReferenceEquals(p1, p2));//ReferenceEquals表示比较对象。而p1、p2是不同的object,所以输出结果为false
Console.WriteLine(Object.Equals(p1, p2));//Equals表示比较数值。而p1、p2有相同的数值(1,2),所以输出结果为true。(自定义的Equals比较数值)
// The line below displays true because p1 and p3 refer to one object.
Console.WriteLine(Object.ReferenceEquals(p1, p3));//p1、p3是相同的object,所以输出结果为true
// The line below displays: p1's value is: (1, 2)
Console.WriteLine("p1's value is: {0}", p1.GetType());
Console.ReadLine();
}
}
}
#endregion
Point类定义Point.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public override bool Equals(Object obj)
{
if (obj.GetType() != this.GetType()) return false;
// obj的强制类型转换~!!!!!!!
Point other = (Point)obj;
return (this.x == other.x) && (this.y == other.y);
}
// Return the XOR of the x and y fields.
public override int GetHashCode()
{
return x ^ y;
}
// Return the point's value as a string.
public override String ToString()
{
return String.Format("({0}, {1})", x, y);
}
// Return a copy of this point object by making a simple field copy.
public Point Copy()
{
return (Point)this.MemberwiseClone();
}
}
}
5.Object应用注意事项
需要注意下面的转换,这是常用转换格式,特地列出,给我提醒:
C c = obj as C;//往往采用这种格式将object类型转化你想要的类型。
举个特例延伸一下,你将obj转为String、Socket、Point等等:
String str = obj as String;
Socket socket = obj as Socket;
Point point = obj as Point; //string、Socket、Point是系统自定的类