Data Type - object

System.Object
This is also called instantiating
First you must declare the object
You allocate memory for an object using the keyword New


You can combine the declaration and the instantiating

object myObject; 
myObject = New MyClass;

Remember: An object variable declared "As New" contains "Nothing" until the first time you refer to it in code


Shallow Copy

The term shallow copying refers to creating a new object and then copying the non-static fields of the current object to the new object
Shallow copying means the copied objects fields will reference the same objects as the original object.
This is performed by the method MemberwiseClone()
If the field is a value type - but by bit copy
If the field is a reference type - the reference is copied


Deep Copy

This is also known as Cloning
The term deep copying refers to creating a new object and then copying the non-static fields of the current object to the new object
Deep cloning means the copied objects fields will reference new copies of the original objects fields
This class must be flagged as [Serializable].
If the field is a value type - bit by bit copy
If the field is a reference type - copy of the object is created with a new reference


Object.Equals


bool b = object.Equals("R", 'R'.ToString());  

Equals actually checks the contents of the objects. ReferenceEquals does not.



Object.ReferenceEquals


class MyClass { 

   static void Main() {
      object a = null;
      object b = null;
      object c = new Object();

      System.Console.WriteLine(Object.ReferenceEquals(a, b));
      p = q;
      System.Console.WriteLine(Object.ReferenceEquals(b, c));
      System.Console.WriteLine(Object.ReferenceEquals(a, b));
   }
}

This code produces True, True, False


Unlike the Equals method and the equality operator, the ReferenceEquals method cannot be overridden.
Because of this, if you want to test two object references for equality and are unsure about the implementation of the Equals method, you can call the ReferenceEquals method.
However, note that if objA and objB are value types, they are boxed before they are passed to the ReferenceEquals method.


Open Generic Types

The typeof operator can also be used on open generic types.
Types with more than one type parameter must have the appropriate number of commas in the specification.


The following example shows how to determine whether the return type of a method is a generic IEnumerable<T>.
Assume that method is an instance of a MethodInfo type:

string s = method.ReturnType.GetInterface ( 
      typeof(System.Collections.Generic.IEnumerable<>).FullName);

System.Object.GetHashCode


Boxing

Boxing is the process of converting a value type into the type object.
This wraps the value inside the System.Object and stores it on the managed heap Boxing is implicit
Forcing value types to act as reference types

int value5 = 23; 
object object1 = value5;

Objects based on nullable types are only boxed when the object is not null.

bool? B = null; 
object o = b;
//o is null

bool? B = true;
object o = b;
//o is true

if (b == null)
{
}


Converting a value type into a reference type is called boxing.


Converting a reference type into a value type is called UnBoxing
The value is "boxed" inside an object and then subsequently "unboxed" back to its value type


Boxing is implicit when you provide a value type and a reference type is expected.
This is an implicit conversion of a value type to a reference type
Boxing a value allocates an instance of an Object and copies the value into the new object instance.
The compiler can detect that you have submitted a value type and silently boxes it within an Object.
You can explicitly cast the value type is you want to.


Explicit

int MyNumber; 
object MyObject;
   MyObject = MyNumber;
   MyObject.ToString();

Implicit

int MyNumber; 
   MyNumber.ToString();


Unboxing

To return a boxed object back to a value type you must explicitly unbox the object.
You can unbox using DirectCast() or the CType() function


UnBoxing extracts the value type from the object Unboxing is explicit


object object2 = 54; 
int value6 = (int)object2;

int myNumber2; 
   if (typeof(Object) Is int)
   {
      Number2 = DirectCast(Object, Integer);
   }

If the Object being boxed is Null or is a reference to an object of a different type an InvalidCastException error occurs.



© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext