Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# Class and Objects
Next article icon

C# | Object Class

Last Updated : 08 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The Object class is the base class for all the classes in the .Net Framework. It is present in the System namespace. In C#, the .NET Base Class Library(BCL) has a language-specific alias which is Object class with the fully qualified name as System.Object. Every class in C# is directly or indirectly derived from the Object class. If a class does not extend any other class then it is the direct child class of the Object class and if extends another class then it is indirectly derived. Therefore the Object class methods are available to all C# classes. Hence Object class acts as a root of the inheritance hierarchy in any C# Program. The main purpose of the Object class is to provide low-level services to derived classes. 

There are two types in C# i.e Reference types and Value types. By using System.ValueType class, the value types inherit the object class implicitly. System.ValueType class overrides the virtual methods from Object Class with more appropriate implementations for value types. In other programming languages, the built-in types like int, double, float, etc. do not have any object-oriented properties. To simulate the object-oriented behavior for built-in types, they must be explicitly wrapped into the objects. But in C#, we have no need for such wrapping due to the presence of value types that are inherited from the System.ValueType that is further inherited from System.Object. So in C#, value types also work similarly to reference types. Reference types directly or indirectly inherit the object class by using other reference types.

  

Explanation of the above Figure: Here, you can see the Object class at the top of the type hierarchy. Class 1 and Class 2 are the reference types. Class 1 is directly inheriting the Object class while Class 2 is Indirectly inheriting by using Class 1. Struct1 is a value type that implicitly inherits the Object class through the System.ValueType type. 

Example: 

CSharp




// C# Program to demonstrate
// the Object class
using System;
using System.Text;
 
class Geeks {
 
    // Main Method
    static void Main(string[] args)
    {
 
        // taking object type
        Object obj1 = new Object();
 
        // taking integer
        int i = 10;
 
        // taking Type type and assigning
        // the value as type of above
        // defined types using GetType
        // method
        Type t1 = obj1.GetType();
        Type t2 = i.GetType();
 
        // Displaying result
 
        Console.WriteLine("For Object obj1 = new Object();");
 
        // BaseType is used to display
        // the base class of current type
        // it will return nothing as Object
        // class is on top of hierarchy
        Console.WriteLine(t1.BaseType);
 
        // It will return the name class
        Console.WriteLine(t1.Name);
 
        // It will return the
        // fully qualified name
        Console.WriteLine(t1.FullName);
 
        // It will return the Namespace
        // By default Namespace is System
        Console.WriteLine(t1.Namespace);
 
        Console.WriteLine();
 
        Console.WriteLine("For String str");
 
        // BaseType is used to display
        // the base class of current type
        // it will return System.Object
        // as Object class is on top
        // of hierarchy
        Console.WriteLine(t2.BaseType);
 
        // It will return the name class
        Console.WriteLine(t2.Name);
 
        // It will return the
        // fully qualified name
        Console.WriteLine(t2.FullName);
 
        // It will return the Namespace
        // By default Namespace is System
        Console.WriteLine(t2.Namespace);
    }
}
 
 

Output:

For Object obj1 = new Object();  Object System.Object System  For String str System.ValueType Int32 System.Int32 System

Constructor

.object-table { border-collapse: collapse; width: 100%; } .object-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .object-table th { border: 1px solid #5fb962; padding: 8px; } .object-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .object-table tr:nth-child(odd) { background-color: #ffffff; } 

Constructor Description
Object() Initializes a new instance of the Object class. This constructor is called by constructors in derived classes, but it can also be used to directly create an instance of the Object class.

Methods

There are total 8 methods present in the C# Object class as follows:

Methods Description
Equals(Object) Determines whether the specified object is equal to the current object.
Equals(Object, Object) Determines whether the specified object instances are considered equal.
Finalize() Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
GetHashCode() Serves as the default hash function.
GetType() Gets the Type of the current instance.
MemberwiseClone() Creates a shallow copy of the current Object.
ReferenceEquals(Object, Object) Determines whether the specified Object instances are the same instance.
ToString() Returns a string that represents the current object.

Important Points:

  • C# classes don’t require to declare the inheritance from the Object class as the inheritance is implicit.
  • Every method defined in the Object class is available in all objects in the system as all classes in the .NET Framework are derived from the Object class.
  • Derived classes can and do override Equals, Finalize, GetHashCode, and ToString methods of Object class.
  • The process of boxing and unboxing a type internally causes a performance cost. Using the type-specific class to handle the frequently used types can improve the performance cost.


Next Article
C# Class and Objects
author
anshul_aggarwal
Improve
Article Tags :
  • C#

Similar Reads

  • C# Class and Objects
    Class and Object are the basic concepts of Object-Oriented Programming which revolve around real-life entities. A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member functions which define actions) into a single uni
    5 min read
  • C# | Method returning an object
    In C#, a method can return any type of data including objects. In other words, methods are allowed to return objects without any compile time error. Example 1: // C# program to illustrate the concept // of the method returning an object using System; class Example { // Private data member private st
    4 min read
  • C# | Object.GetTypeCode() Method with Examples
    This method is used to return the Type of the current instance. Here, Type Represents type declarations i.e. class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types. The System.Object class is
    3 min read
  • C# | Structures | Set - 1
    Structure is a value type and a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types. C# provide the ability to use pre-defined data types. However, sometimes the us
    4 min read
  • C# Properties
    Properties are the special types of class members that provide a flexible mechanism to read, write, or compute the value of a private field. Properties function like public data members but are accessors which makes easy data access. Properties also support encapsulation and abstraction through "get
    5 min read
  • C# Arrays
    An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
    8 min read
  • C# Tuple<T1,T2,T3,T4,T5,T6,T7> Class
    Tuple<T1, T2, T3, T4, T5, T6, T7> class creates a 7-tuple or septuple. It represents a tuple that contains seven elements. We can instantiate a Tuple<T1, T2, T3, T4, T5, T6, T7> object by calling either the Tuple<T1, T2, T3, T4, T5, T6, T7>(T1, T2, T3, T4, T5, T6, T7) constructor o
    3 min read
  • Introduction to C#
    C# (C-sharp) is a modern, object-oriented programming language developed by Microsoft as part of the .NET framework. It was first released in 2000 and it has become one of the most widely used languages for building Windows applications, web services, and more. C# combines the power of C and C++ wit
    7 min read
  • C# Interview Questions and Answers
    C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications. He
    15+ min read
  • C# Abstraction
    Data Abstraction is the property by which only the essential details are shown to the user and non-essential details or implementations are hidden from the user. In other words, Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences