Object クラスとは? わかりやすく解説

Object クラス

.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 クラス定義されているすべてのメソッド使用できます派生クラスでは、これらのメソッドのうち、次のメソッドオーバーライドできます

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

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

1 つ目の方法は、Object 型を受け入れ一般的なメソッドと、クラス頻繁に処理することが予想される値型受け入れる、型固有の一連のメソッド オーバーロード作成することです。呼び出し元のパラメータ型を受け入れる型固有のメソッド存在する場合ボックス化発生せず、その型固有のメソッド呼び出されます。呼び出し元のパラメータ型に一致するメソッド引数ない場合には、パラメータボックス化され、一般的なメソッド呼び出されます。この方法では、CLS 準拠メソッド生成されます。

もう 1 つ方法は、ジェネリック使用するようにクラスとそのメソッドデザインすることです。クラスインスタンス作成しジェネリック型引数指定すると、共通言語ランタイムによってクローズ ジェネリック型作成されます。ジェネリック メソッドは型固有のメソッドであるため、呼び出しパラメータボックス化せずに呼び出すことができますこの方法では、.NET Framework Version 2.0CLS準拠しないメソッド生成されます。

使用例使用例

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
   派生クラス
スレッド セーフスレッド セーフ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からObject クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からObject クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からObject クラス を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Object クラス」の関連用語

Object クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Object クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS