Introduction
In my opinion, learning the C# language is fascinating. I hope you agree, especially to those who have been using it for a while in their career. I still remember back when I was starting out, I noticed that when I typed anything within Visual Studio, it seems that everything behaves like an object. To elaborate further, for example when you have typed a literal number and magically you can invoke the ToString() method after you have typed dot (.).
In this blog post, we are going to discuss the System.Object class. Thus, in this post we are going to tackle the following topics:
- What is the System.Object class?
- Summarized purposes of the object class methods
What is the System.Object class?
All .NET classes are ultimately derived from the System.Object class. In fact, when you don’t specify a base class while defining a class, the compiler automatically assumes that it derives from System.Object. Lastly, because of this behavior, you have access to many public, protected member methods that have been defined for the Object class.
To prove some of the statements above, I have created a unit test to prove the following:
- System.Object is the parent of all classes within the .NET.
- All class builtin and/or defined is a subclass of System.Object
See the sample code below:
- [Fact]
- public void UnitTest_Get_Base_Class_Of_DotNet_Classes()
- {
- //get the assembly name of .net core
- string assemblyFullName = "System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";
- //load the assembly
- Assembly assembly = Assembly.Load(assemblyFullName);
- //filter base on the following criteria
- // Doesn't have a base type because System.Object is the root of all types within the .NET Core
- // System.Object is a class and ansiclass
- TypeInfo baseOfAllClasses = assembly.DefinedTypes.FirstOrDefault(x => x.BaseType == null && x.IsClass && x.IsAnsiClass);
- string objectAlias = string.Empty;
- using (var provider = new CSharpCodeProvider())
- {
- objectAlias = provider.GetTypeOutput(new CodeTypeReference(baseOfAllClasses));
- }
- Assert.True(objectAlias == "object");
- }
- [Fact]
- public void UnitTest_Check_Base_Class_Of_System_Object()
- {
- object obj = new object(); //create new instance of object
- Assert.IsType<object>(obj); //check if object
- Type objType = obj.GetType();
- Assert.Null(objType.BaseType); //doesn't have a base type
- Assert.True(objType.Name == "Object");
- Assert.True(objType.FullName == "System.Object");
- Assert.True(objType.Namespace == "System");
- Assert.True(objType.IsAnsiClass);//is ansiclass
- Assert.True(objType.IsClass);//is class
- }
Above, we have created a new instance of System.Object and checked some properties to see if they're valid.


Raju PaladiyaPosted Feb 17, 2020, 2:13 AM
Nice one !!
Rajanikant HawaldarPosted Feb 16, 2020, 8:34 PM
Informative blog