Object 类

支持 .NET Framework 类层次结构中的所有类,并为派生类提供低级别服务。这是 .NET Framework 中所有类的最终基类;它是类型层次结构的根。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDual)> _
Public Class Object
用法
Dim instance As Object
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDual)] 
public class Object
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDual)] 
public ref class Object
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDual) */ 
public class Object
SerializableAttribute 
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDual) 
public class Object

备注

语言通常不要求类声明从 Object 的继承,因为继承是隐式的。

因为 .NET Framework 中的所有类均从 Object 派生,所以 Object 类中定义的每个方法可用于系统中的所有对象。派生类可以而且确实重写这些方法中的某些,其中包括:

  • Equals — 支持对象间的比较。

  • Finalize — 在自动回收对象之前执行清理操作。

  • GetHashCode — 生成一个与对象的值相对应的数字以支持哈希表的使用。

  • ToString — 生成描述类的实例的可读文本字符串。

性能注意事项

如果要设计的类(如集合)必须处理所有类型的对象,则可以创建接受 Object 类的实例的类成员。但是,对类型进行装箱和取消装箱的过程会增加性能开销。如果知道新类将频繁处理某些值类型,则可以使用下列两种策略之一,以使装箱开销减至最少。

一种策略是创建一个一般方法和一组类型特定的重载方法;一般方法接受 Object 类型,重载方法接受类预期要频繁处理的所有值类型。如果某个类型特定的方法可接受调用参数类型,调用该类型特定的方法时则无需进行装箱。如果调用参数类型与方法参数都不匹配,则对该参数进行装箱并调用一般方法。这种策略产生的方法符合 CLS。

另一种策略是将类及其方法设计为使用泛型。在创建类的实例并指定一个泛型类型参数时,公共语言运行库创建一个封闭泛型类型。泛型方法是类型特定的,无需对调用参数进行装箱即可调用。在 .NET Framework 2.0 版中,这种策略生成的方法不符合 CLS。

示例

下面的示例定义一个派生自 Object 类的 Point 类型,然后重写 Object 类的很多虚方法。此外,该示例还演示如何调用 Object 类的很多静态方法和实例方法。

using System;

// The Point class is derived from System.Object.
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 this and obj do not refer to the same type, then they are not equal.
        if (obj.GetType() != this.GetType()) return false;

        // Return true if  x and y fields match.
        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();
    }
}

public sealed class App {
    static void Main() 
    {
        // Construct a Point object.
        Point p1 = new Point(1,2);

        // Make another Point object that is a copy of the first.
        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));

        // The line below displays true because p1 and p2 refer to two different objects that have the same value.
        Console.WriteLine(Object.Equals(p1, p2));
      
        // The line below displays true because p1 and p3 refer to one object.
        Console.WriteLine(Object.ReferenceEquals(p1, p3));
        
        // The line below displays: p1's value is: (1, 2)
        Console.WriteLine("p1's value is: {0}", p1.ToString());
    }
}

// This code produces the following output.
//
// False
// True
// True
// p1's value is: (1, 2)
using namespace System;

// The Point class is derived from System.Object.
ref class Point
{
public:
    int x;
public:
    int y;

public:
    Point(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

public:
    virtual bool Equals(Object^ obj) override
    {
        // If this and obj do not refer to the same type,
        // then they are not equal.
        if (obj->GetType() != this->GetType())
        {
            return false;
        }

        // Return true if  x and y fields match.
        Point^ other = (Point^) obj;
        return (this->x == other->x) && (this->y == other->y);
    }

    // Return the XOR of the x and y fields.
public:
    virtual int GetHashCode() override 
    {
        return x ^ y;
    }

    // Return the point's value as a string.
public:
    virtual String^ ToString() override 
    {
        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();
    }
};

int main()
{
    // Construct a Point object.
    Point^ p1 = gcnew Point(1, 2);

    // Make another Point object that is a copy of the first.
    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));

    // The line below displays true because p1 and p2 refer
    // to two different objects that have the same value.
    Console::WriteLine(Object::Equals(p1, p2));

    // The line below displays true because p1 and 
    // p3 refer to one object.
    Console::WriteLine(Object::ReferenceEquals(p1, p3));

    // The line below displays: p1's value is: (1, 2)
    Console::WriteLine("p1's value is: {0}", p1->ToString());
}

// This code produces the following output.
//
// False
// True
// True
// p1's value is: (1, 2)

继承层次结构

System.Object
   派生类

线程安全

此类型的公共静态(在 Visual Basic 中为 Shared)成员对于多线程操作是安全的。不能保证实例成员是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Object 成员
System 命名空间