The Basics of C#: A "HelloWorld" Application - HelloWorld source code explained
(Page 3 of 5 )
The HelloWord sample we created on the last page is fairly elementary, yet precisely outlines the structure of a simple C# application:
using library_name;
namespace namespace_name
{
class class_name
{
static void Main([string[] args])
{
// Application code goes here
}
}
}Let's take a look at what each these lines mean in relation to our C# application as a whole:
using library_name;C# is based on the concept of using namespaces. Namespaces separate code into individually named-containers and avoids the chance of having duplicate variable names within the scope of a block. The "using" command allows us to specify which libraries namespaces we want access to. "using" commands are in no way similar to C++'s "#include" directive, and do not physically include any other source code into our file, they simply give us access to the namespaces available in the libraries that we have specified.
The "System" library contains the namespaces for all common .NET variables such as System.Int32 (which represents an integer) amongst other things.
Note: For any language to be compatible with .NET, it must provide implementations of the base .NET variable types. These include Int32, Int64, etc, C# provides its own version of these base variables in a form that resembles C++'s variable types: ushort, int, string, etc.
If we reference the System library with a "using" command at the top of our source file like this:
using System;
... and want to create a new integer variable, then we don't need to explicitly declare Int32 as part of the System namespace, because we already have access to the system libraries namespace:
Int32 myNum = 10;
On the other hand, if we didn't include a "using" command to reference the system library, then we would have to explicitly specify which namespace the Int32 class resides in, like this:
System.Int32 myNum = 10;
Although .NET contains several pre-defined namespaces, we can create our own using a namespace block, like this:
namespace myNS
{
System.Int32 x = 10;
}
This would create a new namespace named "myNS" under the global section of our C# application. To reference the integer variable x from within our new "myNS" namespace, we would use the following syntax:
myNS.x;
To access variables, objects, or other namespaces (namespaces can be nested) from within a namespace, you call the namespaces in the order that they are nested and separate each one with a dot operator. If we have three nested namespaces like this:
namespace ns1
{
namespace ns2
{
namespace ns3
{
System.Int32 number;
}
}
}
... then we can retrieve the value of the "number" integer with a command like this:
ns1.ns2.ns3.number;
Although a namespace isn't required in our sample application, it helps to physically differentiate our code from any other code that might be present in the same source code file.
class class_name
{
}
Just like in C++, C# uses classes to facilitate its object-orientated design methodology. Classes allow us to create individual self-contained units of code, which can be instantiated throughout our application. All C# applications must contain at least one class. If we wanted to create a class named "myClass", then we would use the following commands:
class myClass
{
// Methods and members for this class go here
}
All C# applications must have a Main() function, which is the entry point for execution of the application:
static void Main([string[] args])
{
// Application code goes here
}
As you can see, the "string[] args" argument list is optional, and can be used to access the parameters passed to your executable from the command line. The Main() function is declared as public by default and can return either an integer variable type or nothing, with "void". To explicitly specify our Main() function as public and to return an integer variable, we would use the following Main() function:
public static int Main()
{
}
Next: Simple C# language features >>
More C# Articles
More By James Yang