|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This article has been excerpted from book "Visual C# Programmer's Guide"
Definition
Microsoft touts its new, object-oriented language, C#, as the best language for writing Microsoft .NET applications. C# provides the rapid application development found in Visual Basic with the power of C++. C# syntax is similar to C++ syntax. Some experts also say that C# is Microsoft's answer to Sun Microsystems' Java and Borland's Delphi.
Revision of "Hello World"
Listing 5.1 provides another quick look at the classic "Hello World" application.
Listing 5.1: HelloWorld.cs, Hello World Example
using System; class HelloWorld { static void Main() { Console.WriteLine("Hello, World"); Console.WriteLine("Press any key to continue"); Console.ReadLine(); } }
You can copy the code in Listing 5.1 into any editor such as Notepad and save the file as HelloWorld.cs. To compile the program, type the following at the command line:
csc HelloWorld.cs
Then run the program by typing HelloWorld at command line. The program output is shown in Figure 5.1.
Figure 5.1: Hello World Example Output
As discussed in Chapters 3 and 4, using the System statement makes the system namespace and its classes available in the program. The class statement followed by the name of the class defines a new class. Every program should have at least one Main method, which is the entry point of the application.
The System.Console class defines functionality to read from and write to the system console. The Console.WriteLine method writes a string to the console.
Object Class and Types
In .NET, the Object class is the root of all types. All types are implicitly derived from this class so they have access to the methods defined in the Object class. These methods are described in the Table 5.1.
Method |
Description |
Equals |
Compares whether two object instances are equal. Returns true if two objects are equal; otherwise, returns false. |
ReferenceEquals |
Compares two object instances. Returns true if both are same instances; otherwise, returns false. |
GetHashCode |
Returns a hash code for the object. |
GetType |
Returns a Type object, which holds the types of current instance. |
ToString |
Converts an instance to a string type and returns a String object. | Table 5.1: The Object Class Methods and Their Descriptions
Type Information
The GetType method of the Object class returns a Type object. The Type class is useful when you need to know the internal details of a class such as the type of the class, its attributes, methods, properties, globally unique identifier, name, and fullname. Listing 5.2 demonstrates use of the GetType method.
Listing 5.2: Type Information Example
AClass cls1 = new AClass(); BClass cls2 = new BClass(); Type type1 = cls1.GetType(); Type type2 = cls2.GetType(); Console.WriteLine(type1.BaseType); Console.WriteLine(type1.Name); Console.WriteLine(type1.FullName); Console.WriteLine(type1.Namespace);
You can use many other methods (GetFields, GetEvents, GetInterfaces, etc.) and properties to obtain more information about an object.
Comparing Two Objects
The Object class's Equals and ReferenceEquals methods can be used to compare two objects and their instances, respectively. Listing 5.3 shows one such use of the Equals method.
Listing 5.3: Compare.cs, Compare Two Objects Example
using System;
// Define A Class public class AClass : Object { private void AMethod() { Console.WriteLine("A method"); } }
// Define B Class public class BClass : AClass { private void BMethod() { Console.WriteLine("B method"); } }
public class Test { public static void Main() { AClass cls1 = new AClass(); BClass cls2 = new BClass(); string str1 = "Test"; string str2 = "Test"; Console.WriteLine(Object.Equals(cls1, cls2)); Console.WriteLine(Object.Equals(str1, str2)); Console.WriteLine("Press any key to continue"); Console.ReadLine(); } }
Figure 5.2 shows the result of the program in Listing 5.3.
Figure 5.2: Screen Generated by Listing 5.3.
You use ReferenceEquals in the same manner, as shown:
Console.WriteLine(Object.ReferenceEquals(obj1, obj2));
Convert to a String Type
The ToString method of the Object class converts a type to a string type. Because converting to a string type is a very common programming practice, Microsoft defined the ToString method in the Object class, thus making it available to each type in the .NET world through inheritance. The example code in Listing 5.4 converts an integer type and a floating-point number type to a string type.
Listing 5.4: ToString.cs, Convert to String Example
using System; public class FloatTest { public static void Main() { int i = 12; float flt = 12.005f; Console.WriteLine(i.ToString()); Console.WriteLine(flt.ToString()); } }
Figure 5.3: Screen Generated by Listing 5.4.
Hash Code
You don't use a hash code in regular programming practices, but it's useful if you want to use a type in hashing algorithms such as a hash table. A hash table provides a quick lookup, much like a dictionary. Each member of the hash table has a key value and the object can be referenced by using the key. All members of a class have a memory area defined in the hash table of that class. Members with different names can have the same values but different hash code. By using the method GetHashCode, .NET gives you the flexibility to access the hash code of an object directly and work with it. A derived class can override this method. It's not necessary to return the same hash code for two objects referencing the same value.
Conclusion
See other articles on the website on .NET and C#.
|