Object クラス

定義

.NET クラス階層内のすべてのクラスをサポートし、派生クラスに低レベルのサービスを提供します。 これは、すべての .NET クラスの最終的な基底クラスです。これは型階層のルートです。

public ref class System::Object
public class Object
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
[System.Serializable]
public class Object
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Object
type obj = class
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)>]
[<System.Serializable>]
type obj = class
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type obj = class
Public Class Object
属性

次の例では、 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.
        var 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 $"({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.
        var p1 = new Point(1,2);

        // Make another Point object that is a copy of the first.
        var p2 = p1.Copy();

        // Make another variable that references the first Point object.
        var 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: {p1.ToString()}");
    }
}

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

// The Point class is derived from System.Object.
type Point(x, y) =
    member _.X = x
    member _.Y = y
    override _.Equals obj =
        // If this and obj do not refer to the same type, then they are not equal.
        match obj with
        | :? Point as other ->
            // Return true if  x and y fields match.
            x = other.X &&  y = other.Y
        | _ -> 
            false

    // Return the XOR of the x and y fields.
    override _.GetHashCode() =
        x ^^^ y

    // Return the point's value as a string.
    override _.ToString() =
        $"({x}, {y})"

    // Return a copy of this point object by making a simple field copy.
    member this.Copy() =
        this.MemberwiseClone() :?> Point

// Construct a Point object.
let p1 = Point(1,2)

// Make another Point object that is a copy of the first.
let p2 = p1.Copy()

// Make another variable that references the first Point object.
let p3 = p1

// The line below displays false because p1 and p2 refer to two different objects.
printfn $"{Object.ReferenceEquals(p1, p2)}"

// The line below displays true because p1 and p2 refer to two different objects that have the same value.
printfn $"{Object.Equals(p1, p2)}"

// The line below displays true because p1 and p3 refer to one object.
printfn $"{Object.ReferenceEquals(p1, p3)}"

// The line below displays: p1's value is: (1, 2)
printfn $"p1's value is: {p1.ToString()}"
// This code example produces the following output:
//
// False
// True
// True
// p1's value is: (1, 2)
//
' The Point class is derived from System.Object.
Class Point
    Public x, y As Integer
    
    Public Sub New(ByVal x As Integer, ByVal y As Integer) 
        Me.x = x
        Me.y = y
    End Sub
    
    Public Overrides Function Equals(ByVal obj As Object) As Boolean 
        ' If Me and obj do not refer to the same type, then they are not equal.
        Dim objType As Type = obj.GetType()
        Dim meType  As Type = Me.GetType()
        If Not objType.Equals(meType) Then
            Return False
        End If 
        ' Return true if  x and y fields match.
        Dim other As Point = CType(obj, Point)
        Return Me.x = other.x AndAlso Me.y = other.y
    End Function 

    ' Return the XOR of the x and y fields.
    Public Overrides Function GetHashCode() As Integer 
        Return (x << 1) XOR y
    End Function 

    ' Return the point's value as a string.
    Public Overrides Function ToString() As String 
        Return $"({x}, {y})"
    End Function

    ' Return a copy of this point object by making a simple field copy.
    Public Function Copy() As Point 
        Return CType(Me.MemberwiseClone(), Point)
    End Function
End Class  

NotInheritable Public Class App
    Shared Sub Main() 
        ' Construct a Point object.
        Dim p1 As New Point(1, 2)
        
        ' Make another Point object that is a copy of the first.
        Dim p2 As Point = p1.Copy()
        
        ' Make another variable that references the first Point object.
        Dim p3 As Point = 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: {p1.ToString()}")
    
    End Sub
End Class
' This example produces the following output:
'
' False
' True
' True
' p1's value is: (1, 2)
'

注釈

Object クラスは、すべての .NET クラスの最終的な基底クラスです。これは型階層のルートです。

.NET のすべてのクラスは Objectから派生しているため、 Object クラスで定義されているすべてのメソッドは、システム内のすべてのオブジェクトで使用できます。 派生クラスでは、次のようなメソッドの一部をオーバーライドできます。

  • Equals: オブジェクト間の比較をサポートします。
  • Finalize: オブジェクトが自動的に再利用される前にクリーンアップ操作を実行します。
  • GetHashCode: ハッシュ テーブルの使用をサポートするために、オブジェクトの値に対応する数値を生成します。
  • ToString: クラスのインスタンスを記述する人間が判読できるテキスト文字列を作成します。

通常、言語では継承が暗黙的であるため、 Object からの継承を宣言するクラスは必要ありません。

パフォーマンスに関する考慮事項

任意の種類のオブジェクトを処理する必要があるクラス (コレクションなど) を設計する場合は、 Object クラスのインスタンスを受け入れるクラス メンバーを作成できます。 ただし、型のボックス化とボックス化解除のプロセスでは、パフォーマンス コストが発生します。 新しいクラスが特定の値の型を頻繁に処理することがわかっている場合は、2 つの方法のいずれかを使用して、ボックス化のコストを最小限に抑えることができます。

  • Object型を受け入れる一般的なメソッドと、クラスが頻繁に処理する必要がある各値型を受け入れる型固有のメソッド オーバーロードのセットを作成します。 呼び出し元のパラメーター型を受け入れる型固有のメソッドが存在する場合、ボックス化は行われず、型固有のメソッドが呼び出されます。 呼び出し元のパラメーター型に一致するメソッド引数がない場合、パラメーターはボックス化され、一般的なメソッドが呼び出されます。
  • ジェネリックを使用するように型とそのメンバーを設計 します。 共通言語ランタイムは、クラスのインスタンスを作成し、ジェネリック型引数を指定するときに、閉じたジェネリック型を作成します。 ジェネリック メソッドは型固有であり、呼び出し元のパラメーターをボックス化せずに呼び出すことができます。

Object型を受け入れて返す汎用クラスを開発する必要がある場合もありますが、頻繁に使用される型を処理する型固有のクラスを提供することで、パフォーマンスを向上させることができます。 たとえば、ブール値の設定と取得に固有のクラスを指定すると、ブール値のボックス化とボックス化解除のコストが削減されます。

コンストラクター

名前 説明
Object()

Object クラスの新しいインスタンスを初期化します。

メソッド

名前 説明
Equals(Object, Object)

指定したオブジェクト インスタンスが等しいと見なされるかどうかを判断します。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

Finalize()

オブジェクトがガベージ コレクションによって解放される前に、リソースを解放し、その他のクリーンアップ操作を実行できるようにします。

GetHashCode()

既定のハッシュ関数として機能します。

GetType()

現在のインスタンスの Type を取得します。

MemberwiseClone()

現在の Objectの簡易コピーを作成します。

ReferenceEquals(Object, Object)

指定した Object インスタンスが同じインスタンスであるかどうかを判断します。

ToString()

現在のオブジェクトを表す文字列を返します。

適用対象

スレッド セーフ

この型のパブリック静的 (Visual Basic のShared ) メンバーはスレッド セーフです。 インスタンス メンバーがスレッド セーフであるとは限りません。