Now that you’ve finished your first C# program, it’s time to talk about the intricacies of the C# language. In this section, I’ll discuss the C# syntax and components and how to use them.
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);
}
}
}
No comments:
Post a Comment