Monday 28 November 2011

The Object Class


­­­­­­­­­­­­­­As described, in the .NET framework, all types are represented as objects and are derived from the Object class. The Object class defines five methods: Equals, ReferenceEquals GetHashCode, GetType and ToString. Table 2 describes these methods, which are available to all types in the .NET library.

Table 2. Object class methods

METHOD
DESCRIPTION
GetType
Return type of the object.
Equals
Compares two object instances. Returns true if they’re Equal; otherwise false.
ReferenceEquals
Compares two object instances. Returns true if both are Same instance; otherwise false.
ToString
Converts an instance to a string type.
GetHashCode
Return hash code for an object.



The following sections discuss the object class methods in more detail.

The GetType method


You can use the Type class to retrieve type information from the object. The GetType method of an object return a type object, which you can use to get information on an object such as its name, namespace, base type, and so on. Listing 6 retrieves the information of objects. In Listing 6, you get the type of the Object and System.String classes.

 

Listing 6 GetType example


using System;
class TypeClass
{
static void Main(string [] args)
{
//create object of type object and string
Object cls1 = new Object ();
System.String cls2 = "Test string";
// Call Get Type to return the type
Type type1 = cls1.GetType( );
Type type2 =cls2.GetType( );
// Object class output
Console.WriteLine(type1.BaseType);
Console.WriteLine(type1.Name);
Console.WriteLine(type1.FullName);
Console.WriteLine(type1.Namespace);

// String output
Console.WriteLine(type2.BaseType);
Console.WriteLine(type2.Name);
Console.WriteLine(type2.FullName);
Console.WriteLine(type2.Namespace);
}
}

Figure 3 shows the output of listing 6.


Figure 3. Output of listing

The Equals and ReferenceEqual Methods


The Equals method in the Object class can compare two objects. The ReferenceEqual method can compare the two objects’ instances. For example:

Console.WriteLine(Object.Equals(cls1, cls2));
Console.WriteLine(Object.Equals(str1, str2));

See listing 7 get type, equal, and reference Equals

Listing 7. Get Type, Equal, and ReferenceEquals

using System;
namespace TypesSamp
{
//define class 1
public class Class1: object
{
private void Method1()
{
 Console.WriteLine("1 method");
}
}

// Define class 2
public class Class2: Class1
{
private void Method2( )
{
 Console.WriteLine("2 method");
}
}

class TypeClass
{
static void Main(string [] args)
{
Class1 cls1 = new Class1();
Class2 cls2 = new Class2();
Console.WriteLine ("= = = = = = = = = = ");
Console.WriteLine ("Type Information");
Console.WriteLine ("= = = = = = = = = =");
// Getting type information
Type type1 =cls1.GetType( );
Type type2 = cls2.GetType( );
Console.WriteLine(type1.BaseType);
Console.WriteLine(type1.Name);
Console.WriteLine(type1.FullName);
Console.WriteLine(type1.Namespace);

// Comparing two objects
string str1 = "Test";
string str2 = "Test";
Console.WriteLine(" = = = = = = = = = = = ");
Console.WriteLine("comparison of two objects");
Console.WriteLine(object.Equals(cls1, cls2));
Console.WriteLine(object.Equals(str1, str2));
}
}
}

Figure 4 shows the output of listing 7.


Figure 4 get type and compare objects code output


The ToString Method and String Conversion


The ToString method of the Object class converts a type to a string type.


Listing 8 shows an example of the ToString method.

 

Listing 8. ToString method example


using System;
namespace ToStringSamp
{
class Test
{
static void Main(string [] args)
{
  int num1 =8;
  float num2 =162.034f;
  Console.WriteLine(num1.ToString( ));
  Console.WriteLine(num2.ToString( ));
}
 }
}

The GetHashCode method


A hashtable (also commonly known as a map or dictionary) is a data structure that stores one or more key- value pairs of data. Hashtables are useful when you want fast access to a list of data through a key (which can be a number, letter, string, or any object). In .NET the HashTable class represents a hashtable, which is implemented based on a hashing algorithm. This class also provides methods and constructors to define the size of the hash table. You can use the Add and Remove methods to add and remove items from a hashtable. The Count property of the HashTable class returns the number of items in a hashtable.

The GetHashCode method returns the hash code of an object. To return a hash code for a type, you must override the GetHashCode method. An integer value is returned, which represents whether an object is available in a hashtable.

Two other useful methods of the object class are MemberWiseClone and Finalize methods. The MemberWiseClone method creates a shallow copy of an object, which can be used as a clone of an object. The Finalize method acts as a destructor and can clean up the resources before the garbage collector calls the object. You need to override this method and write your own code to clean up the resources. The garbage collector automatically calls the Finalize method if an object is no longer in use.

 

No comments:

Post a Comment