Object クラス
アセンブリ: 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

通常、各言語では、クラスの Object の継承を宣言する必要はありません。継承が暗黙的であるためです。
.NET Framework では、すべてのクラスが Object から派生するため、システム内のオブジェクトは、Object クラスで定義されているすべてのメソッドを使用できます。派生クラスでは、これらのメソッドのうち、次のメソッドをオーバーライドできます。
オブジェクトの任意の型を処理する必要のあるクラス (コレクションなど) をデザインする場合、Object クラスのインスタンスを受け入れるクラス メンバを作成できます。ただし、型のボックス化およびボックス化解除のプロセスによって、パフォーマンス コストが発生します。新しいクラスで特定の値型を頻繁に処理することがわかっている場合は、次の 2 つの方法のいずれかを使用することで、ボックス化のコストを最小限に抑えることができます。
1 つ目の方法は、Object 型を受け入れる一般的なメソッドと、クラスで頻繁に処理することが予想される各値型を受け入れる、型固有の一連のメソッド オーバーロードを作成することです。呼び出し元のパラメータ型を受け入れる型固有のメソッドが存在する場合、ボックス化は発生せず、その型固有のメソッドが呼び出されます。呼び出し元のパラメータ型に一致するメソッドの引数がない場合には、パラメータはボックス化され、一般的なメソッドが呼び出されます。この方法では、CLS 準拠のメソッドが生成されます。
もう 1 つの方法は、ジェネリックを使用するようにクラスとそのメソッドをデザインすることです。クラスのインスタンスを作成し、ジェネリック型の引数を指定すると、共通言語ランタイムによってクローズ ジェネリック型が作成されます。ジェネリック メソッドは型固有のメソッドであるため、呼び出しパラメータをボックス化せずに呼び出すことができます。この方法では、.NET Framework Version 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)


この型の 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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Object コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)



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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Object メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 |
![]() | ToString | 現在の Object を表す String を返します。 |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 |

Object メンバ
.NET Framework クラス階層のすべてのクラスをサポートし、派生クラスに下位レベルのサービスを提供します。これは、.NET Framework の全クラスの基本クラスであり、型階層のルートです。
Object データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 |
![]() | ToString | 現在の Object を表す String を返します。 |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 |

Weblioに収録されているすべての辞書からObjectを検索する場合は、下記のリンクをクリックしてください。

- Objectのページへのリンク