![]() |
Built-In Classes: The Object Class |
|
Introduction |
|
C# was clearly created to improve on C++ and possibly offer a new alternative. To achieve this goal, Microsoft created a huge library to accompany the language. The .NET Framework is a huge library made of various classes and constants you can directly use in your C# application without necessarily explicitly loading an external library. To start, this main library of C# provides a class called Object. As you may have realized by now, every variable or function in C# (as in Java) must belong to a class, unlike C/C++ where you can have global variables or functions. Therefore, you always have to create at least one class for your application. As such, when you create a class, it automatically inherits its primary characteristics from the parent of all classes: Object. |
When you declare and initialize two variables, one of the operations you may want to subsequently perform is to compare their value. To support this operation, the Object class provides its children with a method called Equals. The Equals() method comes in two versions. The first has the following syntax: public virtual bool Equals(object obj); This version allows you to call the Equals() method on a declared variable and pass the other variable as argument. Here is an example: using System;
class BookCollection
{
static void Main()
{
// First book
int NumberOfPages1 = 422;
// Second book
int NumberOfPages2 = 858;
// Third book
int NumberOfPages3 = 422;
if( NumberOfPages1.Equals(NumberOfPages2) == true )
Console.WriteLine("The first and the second books have the same number of pages");
else
Console.WriteLine("The first and the second books have different number of pages");
if( NumberOfPages1.Equals(NumberOfPages3) == true )
Console.WriteLine("The first and the third books have the same number of pages");
else
Console.WriteLine("The first and the third books have different number of pages");
}
}
This would produce: The first and the second books have different number of pages The first and the third books have the same number of pages The first version of the Object.Equals method is declared as virtual, which means you can override it if you create your own class. The second version of the Object.Equals() method is: public static bool Equals(object obj2, object obj2); As a static method, to use it, you can pass the variables of the two classes whose values you want to compare. In both cases, if the values of the variables are similar, the Equals() method returns true. If they are different, the method returns false. If you are using the Equals() method to compare the variables of two primitive types, the comparison should be straight forward. If you want to use this methods on variables declared from your own class, you should provide your own implementation of this method.
In previous lessons, we learned that, to convert the value of a variable declared from a primitive type to a string, you could call the ToString() function. Here is an example: using System;
class BookCollection
{
static int Main()
{
int NumberOfPages = 422;
Console.WriteLine("Number of Pages: {0}", NumberOfPages.ToString());
return 0;
}
}
In many programming languages such as C++, programmers usually have to overload an (extractor) operator to display the value(s) of class' variable to the screen. The Object class provides an alternative to this somewhat complicated solution, through the ToString() method. It syntax is: public virtual string ToString(); Although the Object class provides this method as non abstract, its implemented version is more useful if you use a primitive type such as int, double and their variances or a string variable. The best way to rely on it consists of overriding it in your own class if you desired to use its role.
When we study inheritance, we will learn that all data types used in a C# program are "based on" an object called object. As introduced earlier, you can use this data type to declare a variable that would hold any type of value. Because this is some type of a "universal" data type, it can also be initialized with any value. Here are examples: using System;
class Exercise
{
static void Main()
{
object Number = 244;
object Thing = "Professor Kabba";
Console.WriteLine(Number);
Console.WriteLine(Thing);
}
}
This would produce: 244 Professor Kabba As you can see, when an object variable is initialized, the compiler finds out the type of value that was assigned to it. This is referred to as boxing. This mechanism is transparently done in C# (and in Visual Basic but not in Visual C++ 2003 (it is possible that something will be done in the next version, or not)). If you declare a variable using a primitive data type (int, float, double, etc), at one time, you may be interested in converting the value of that variable into an object. Here is an example: using System;
class Exercise
{
static int Main()
{
int Number = 244;
object Thing = Number;
Console.WriteLine(Number);
Console.WriteLine(Thing);
return 0;
}
}
This would produce: 244 244 This operation is referred to as unboxing. As you can see, this operation is performed transparently (Visual C++ 2003 doesn't do it transparently). Boxing and unboxing make C# a very flexible and wonderful language (if you misuse it, of course it can be dangerous).
While a constructor, created for each class, is used to instantiate a class. The Object class provides the Finalize() method as a type of destructor.
The System namespace provides one of the largest definition of classes of the .NET Framework, but it doesn't contain everything. For example, when you start writing graphical user interface (GUI) applications, you will have to use other namespaces. The namespaces are contained in libraries called assemblies. The actual classes used in various applications are created and defined in these libraries. Before using a class, you must know the name of the assembly in which it is defined. You must also know the name of its namespace. These three pieces of information, the name of the class, the namespace in which it is defined, and the name of the assembly in which the namespace is contained, are very important. Because there are so many classes, namespaces, and libraries, the MSDN documentation is your best reference. We can only mention a few, especially those that are relevant for the subjects we are reviewing. |
|
|
||
| Home | Copyright © 2008 Yevol | |
|
|
||