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 클래스의 인스턴스를 받는 클래스 멤버를 만들 수 있습니다. 그러나 형식을 boxing 및 unboxing하는 과정으로 인해 성능이 떨어집니다. 새 클래스에서 특정 값 형식을 자주 처리하는 경우에는 boxing에 의한 성능 저하를 최소화하기 위해 두 가지 방법 중 하나를 사용할 수 있습니다.

한 가지 방법은 Object 형식을 받는 일반 메서드를 만들고, 클래스에서 자주 처리하는 각각의 값 형식을 받는 형식별 메서드 오버로드를 여러 개 만드는 것입니다. 호출 매개 변수 형식을 받는 형식별 메서드가 있으면 boxing이 발생하지 않고 형식별 메서드가 호출됩니다. 호출 매개 변수 형식과 일치하는 메서드 인수가 없으면 매개 변수가 boxing되고 일반 메서드가 호출됩니다. 이 방법을 사용하면 CLS 규격 메서드가 생성됩니다.

다른 방법은 사용자 클래스와 해당 제네릭 메서드를 디자인하는 것입니다. 사용자가 사용자 클래스의 인스턴스를 만들고 제네릭 형식 인수를 지정하면 공용 언어 런타임에서는 폐쇄형 제네릭 형식을 만듭니다. 제네릭 메서드는 형식에 고유하며 호출 매개 변수를 boxing하지 않고 호출될 수 있습니다. 이 방법을 사용하면 .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
   파생 클래스

스레드로부터의 안전성

이 형식의 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에서 지원

참고 항목

참조

Object 멤버
System 네임스페이스