System.Object

   System이라는 네임스페이스의 Object라는 클래스

   모든 타입의 조상

   기본적으로 object에서 상속받는 가정하에 코드를 생성한다.

 

Object의 메서드 - ToString

   해당 인스턴스가 속한 클래스의 전체 이름 FQDN을 반환

   기본타입의 경우 클래스의 전체 이름이 아닌 값을 반환

   하위클래스에서 재정의 가능

using System;
namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Parent p = new Parent();
            string s = "sample";
            int n = 0;
            Console.WriteLine(p.ToString());
            Console.WriteLine(s.ToString());
            Console.WriteLine(n.ToString());
        }
    }
    class Parent{ }
    class Children : Parent {
        public void Method1()
        {
        }
    }
}

 

Object의 메서드 - GetType

  상속 Object -> MemberInfo -> Type 이므로 class 로 타입을 정의할 경우 System.Type 인스턴스를 보유한다.

   GetType은 타입 전체 이름을 반환

   FullName, IsClass, IsArray 프로퍼티를 가지고 있음

using System;
namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Parent p = new Parent();
            string s = "sample";
            int n = 0;
            Console.WriteLine(p.GetType());
            Console.WriteLine(s.GetType());
            Console.WriteLine(n.GetType());
        }
    }
    class Parent{ }
    class Children : Parent {
        public void Method1()
        {
        }
    }
}

 

Object의 메서드 - Equals

   stack에 할당된 값(참조형식의 경우 힙메모리 위치)를 비교

   값형식 : 값을 대상으로 비교

   참조형식 : 할당된 메모리의 위치를 가리키는 식별자의 값 비교

using System;
namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Parent p = new Parent();
            Parent p1 = new Parent();
            string s = "sample";
            string s1 = "sample";
            int n = 0;
            int n1 = 0;
            Console.WriteLine(p.Equals(p1));
            Console.WriteLine(s.Equals(s1));
            Console.WriteLine(n.Equals(n1));
        }
    }
    class Parent{ }
    class Children : Parent {
        public void Method1()
        {
        }
    }
}

 

 

Object의 메서드 - GetHashCode

   인스턴스를 식별할 수 있는 4바이트의 int값을 반환

   타입이 int인 경우는 값을 반환

   long 타입의 경우 8바이트이기 때문에 값이 다름에도 동일한 해시코드가 반환될 수 있음

   해시 충돌이 발생할 수 있으므로 Equals 와 같이 사용

   GetHashCode 의 반환값이 같으면 Equals로 재확인

 

'프로그래밍 > C#' 카테고리의 다른 글

추상클래스  (0) 2020.03.22
다형성  (0) 2020.03.22
as, is 연산자  (0) 2020.03.20
객체지향 - 3  (0) 2020.03.20
객체지향 -2  (0) 2020.03.20

WRITTEN BY
beautifulhill

,