Creating a Simple Hello World Application

Team-Fly    

 
.NET and COM Interoperability Handbook, The
By Alan Gordon
Table of Contents
Chapter Four.  A Quick Introduction to C#

Creating a Simple Hello World Application

A simple hello world application written in C# follows :

 namespace HelloWorld {     using System;     public class Hello     {         public Hello()         {             // Constructor logic goes here         }         public static int Main(string[] args)         {       Console.WriteLine("Hello: ");          if (args.Length==0)         Console.WriteLine("who ever you are!");       else          for (int i=0;i<args.Length;i++)         Console.Write(" {0} ", args[i]);            return 0;         }     } } 

Let's examine the code a little deeper. First, notice that the code is enclosed within a namespace called HelloWorld

 namespace HelloWorld { //... } 

The class name is Hello , therefore, the full name of the class that I have declared here is called HelloWorld.Hello , that is, the name is [NamespaceName].[ClassName] .

Note

The fact that I even need to declare a class to write my hello world program is a key difference between C++ and C#. C# does not have any global functions. Even the Main function that you write to be the entry point for your application must be a member of a class.


You can declare an instance of this class using the full class name as follows:

 HelloWorld.Hello helloApp; 

or, you can add a "using" statement to enable you to use the classes in the namespace without prepending the namespace name to the class name as follows:

 using HelloWorld; //... Hello helloApp; 

The next statement in the main listing is a using statement for the System namespace. This statement allows us to use the Console class, which is in the System namespace, without having to use the name of its namespace. If you did not add the "using" statement for the System namespace, you would have to substitute System.Console everywhere that you used the Console typename.

Namespaces are just a way to assign a longer (and thus more likely, unique) name to a class so that you avoid naming clashes . Namespaces can be nested, and Microsoft recommends that you use at least two nested layers of namespaces with your company name followed by the technology name and optionally the feature and design as follows:

 CompanyName.TechnologyName[.Feature][.Design] 

Some example names for namespaces are the following:

 Microsoft.Office Microsoft.Office.Charting Microsoft.Office.Charting.Design 

Using this naming convention prevents two published namespaces from having the same name. The first two parts of the namespace name, the CompanyName and the TechnologyName, should be enough to guarantee uniqueness in most cases, particularly if you use your Internet domain name for the company name. The CompanyName and TechnologyName are the only parts of the namespace name that are required. The "Feature" part of the name is optional and can be used to provide an extra hierarchy layer for complex technologies with multiple, related groups of classes. If a library or framework contains types that support design-time functionality, you should enclose the Design- related types in a namespace with "Design" as the innermost name.

Therefore, if you worked for ACME Software in the business objects group , you would probably use ACME as the CompanyName portion of your namespace name and BusinessObjects as the TechnologyName portion of your namespace name as shown here:

 namespace ACME {     namespace BusinessObjects     {     // Your types and other code goes here...      } } 

A short, and better, form for declaring a two-level nested namespace is the following.

 namespace ACME.BusinessObjects { // Your types and other code goes here... } 

The rest of the code in the "hello world" example contains the declaration of a class called Hello . I won't dwell on this definition because there is another section in this chapter that talks about declaring classes in C#. I will talk about the "Main" method in the Hello class though. Much like C and C++, all C# programs will have a "Main" method. This method is the entry point for the application, that is, it's the first user -defined code that the operating system will execute when you run a program. In C#, this method must be declared as a static method that returns an integer. The method takes a string array as its only argument. The CLR will fill this array with the command-line arguments that were passed to the program when it was started. As you can see in the program, you can find out how many arguments were passed to the program using the Length property of the array, and you can access the arguments using the index operator, [].

The "hello world" program also shows you how to do console I/O using the Write and WriteLine methods on the Console class.


Team-Fly    
Top
 


. Net and COM Interoperability Handbook
The .NET and COM Interoperability Handbook (Integrated .Net)
ISBN: 013046130X
EAN: 2147483647
Year: 2002
Pages: 119
Authors: Alan Gordon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net