Namespace and Assemblies
The first line of the “Hello, C# World!”
program was this:
using System;
This line adds a reference to the System namespace to the program. After adding a reference to a
namespace, you can access any member of the namespace. As mentioned, in .NET
library references documentation, each class belongs to a namespace. But what
exactly is a namespace?
To define .NET classes in a category so
they’d be easy to recognize, Microsoft used the C++ class-packaging concept
know as namespaces. A namespace is
simply a grouping of related classes. The root of all namespaces is the System namespace. If you see namespaces in the .NET library, each
class is defined in a group of similar category. For example, The System.Data namespace only possesses data-related classes,
and System.Multithreading contains only
multithreading classes.
When you create a new application using
visual C#, you see that each application is defined as a namespace and that all
classes belong to that namespace. You can access these classes from other
application by referencing their namespaces.
For
example, you can create a new namespace MyOtherNamespace with a method Hello defined in it. The Hello method writes “Hello, C# World!” to
the console. Listing2 shows the namespace.
Listing 2 Namespace wrapper for the hello class
// Called namespace
namespace MyOtherNamespace
{
class MyOtherClass
{
public void Hello()
{
Console.WriteLine ("Hello,
C# World!");
}
}
}
In listing 3, you’ll see how to reference
this namespace and call MyOtherClass’s Hello method
from the main program.
In listing 2, the MyOtherClass and its members can be accessed from other
namespaces by either placing the statement using MyOtherNamespace before the
class declaration or by referring to the class my other namespace before the
class declaration or by referring to the class as MyOtherNamespace.Hello, as shown in listing 3 and listing 4.
Listing 3. Calling my other Namespace Name space members
using System;
using
MyOtherNamespace;
// Caller namespace
namespace HelloWorldNamespace
{
class Hello
{
static void Main()
{
MyOtherClass
cls = new MyOtherClass();
cls.Hello();
}
}
}
// Called namespace
namespace MyOtherNamespace
{
class MyOtherClass
{
public void Hello()
{
Console.WriteLine("Hello,
C# World!");
}
}
}
As you have seen in listing 3, you include
a namespace by adding the using directly. You can also reference a namespace
direct without the using directive.
Listing 4 shows you how to use MyOtherClass of MyOtherNamespace.
Listing 4. Calling the HelloWorld namespace member from the MyOtherNamespace
// Caller namespace
namespace HelloWorldNamespace
{
class Hello
{
static void Main()
{
MyOtherNamespace.MyOtherClass cls =
new
MyOtherNamespace.MyOtherClass();
cls.Hello();
}
}
}
Standard Input and Output Streams
The System.Console class provides the
capability to read streams from and write streams to the System console. It also defines
functionality for error streams. The Read operation reads data from the console
to the standard input stream, and the Write operation writes data to the
standard output stream. The standard error stream is responsible for storing
error data. These streams are the automatically associated with the system
console.
The error, in, and out
properties of the Console class
represents standard error output, standard input and standard output streams.
In the standard output stream, the Read method reads the next character, and
the ReadLine method reads the next line. The Write and WriteLine methods write the data to the standard output
stream. Table 1 describes some of the console class methods.
Table 1. The System.Console Class methods
METHOD
|
DESCRIPTION
|
EXAMPLE
|
Read
|
Reads a single character
|
int i = Console.Read();
|
ReadLline
|
Reads a line
|
string str = Console.ReadLine();
|
Write
|
Writes a line
|
Console.Write ("Write: 1");
|
WriteLine
|
Writes a line followed by a line
terminator
|
Console.WriteLine("Test Output Data
with Line");
|
|
|
|
Listing 5 shows you how to use the Console class and its members
Listing 5. Console class example
using System;
namespace ConsoleSamp
{
class Classs1
{
static void Main(string[
] args )
{
Console.Write("Standard I/O
Sample");
Console.WriteLine("");
Console.WriteLine ("= = = = = = = =
");
Console.WriteLine ("Enter your name . .
.");
string name =
Console.ReadLine();
Console.WriteLine("Output: Your name is :
"+ name);
}
}
}
Figure2 shows the output of listing 5.
Figure 2. The console class methods output
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 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