.NET Framework クラス階層のすべてのクラスをサポートし、派生クラスに下位レベルのサービスを提供します。これは、.NET Framework の全クラスの基本クラスであり、型階層のルートです。
名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDual)> _
Public Class 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 クラスのインスタンスを受け入れるクラス メンバを作成できます。ただし、型のボックス化およびボックス化解除のプロセスによって、パフォーマンス コストが発生します。新しいクラスで特定の値型を頻繁に処理することがわかっている場合は、次の 2 つの方法のいずれかを使用することで、ボックス化のコストを最小限に抑えることができます。
1 つ目の方法は、Object 型を受け入れる一般的なメソッドと、クラスで頻繁に処理することが予想される各値型を受け入れる、型固有の一連のメソッド オーバーロードを作成することです。呼び出し元のパラメータ型を受け入れる型固有のメソッドが存在する場合、ボックス化は発生せず、その型固有のメソッドが呼び出されます。呼び出し元のパラメータ型に一致するメソッドの引数がない場合には、パラメータはボックス化され、一般的なメソッドが呼び出されます。この方法では、CLS 準拠のメソッドが生成されます。
もう 1 つの方法は、ジェネリックを使用するようにクラスとそのメソッドをデザインすることです。クラスのインスタンスを作成し、ジェネリック型の引数を指定すると、共通言語ランタイムによってクローズ ジェネリック型が作成されます。ジェネリック メソッドは型固有のメソッドであるため、呼び出しパラメータをボックス化せずに呼び出すことができます。この方法では、.NET Framework Version 2.0 の CLS に準拠しないメソッドが生成されます。
Object クラスから派生した Point 型を定義し、Object クラスの仮想メソッドの多くをオーバーライドする例を次に示します。また、この例は、Object クラスの多数の静的メソッドとインスタンス メソッドを呼び出す方法も示しています。
Imports System
' 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 'New
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 'Equals
' Return the XOR of the x and y fields.
Public Overrides Function GetHashCode() As Integer
Return x ^ y
End Function 'GetHashCode
' Return the point's value as a string.
Public Overrides Function ToString() As String
Return String.Format("({0}, {1})", x, y)
End Function 'ToString
' 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 'Copy
End Class 'Point
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: {0}", p1.ToString())
End Sub 'Main
End Class 'App
' This code example produces the following output:
'
' False
' True
' True
' p1's value is: (1, 2)
'
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 example 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
派生クラス
この型の public static (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