What are object data types in C#?

CsharpProgrammingServer Side Programming

The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.

The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class.

When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.

The following is an example −

object obj;
obj = 100; // this is boxing

Here is the complete example showing the usage of object data types −

Example

 Live Demo

using System;
using System.IO;

namespace Demo {
   class objectClass {
      public int x = 200;
   }

   class MyApplication {
      static void Main() {
         object obj;
         obj = 50;
         Console.WriteLine(obj);
         Console.WriteLine(obj.GetType());
         Console.WriteLine(obj.ToString());
   
         obj = new objectClass();
         objectClass newRef;
         newRef = (objectClass)obj;
         Console.WriteLine(newRef.x);
      }
   }
}

Output

50
System.Int32
50
200
raja
Published on 10-Aug-2018 17:27:42
Advertisements