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);


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