Let’s begin by looking at the structure of a C# application. Every C# application contains certain elements. The application begins with the hierarchical levels, listed here in order of appearance.

  1. Namespace
  2. Class
  3. Method

Every application begins with a namespace that has the same name as the project. Of course, you can change the namespace to anything you like in order to maintain compatibility with other projects.

For example we declared namespace Mycplus.CSharpBasics while taking an Overview of C#. We cam write the whole application with in one namespace or we can declare multiple namespaces as needed in our project. The general syntax of declaring a namespace is

Below the namespace level is the class level. The name of this class varies. If you create a console application, then C# assigns the class a name of Class1; Windows applications begin with a class name of Form1. But it is a good practise to name your class with the functionality it performs.

Classes normally contain one or more methods. Every C# application has at least one method in it called Main(). This is the method that C# uses as a starting point for execution. Every C# application has to have an entry point, and some types of C# projects often have more than one. For example, a DLL commonly has
more than one entry point.

Application Structure

Let’s begin by looking at the structure of a C# application. Every C# application contains certain elements. The application begins with the hierarchical levels, listed here in order of appearance.

  • Namespace
  • Class
  • Method

Every application begins with a namespace that has the same name as the project. Of course, you can change the namespace to anything you like in order to maintain compatibility with other projects.

Below the namespace level is the class level. The name of this class varies. If you create a console application, then C# assigns the class a name of Class1; Windows applications begin with a class name of Form1.

Classes normally contain one or more methods. Every C# application has at least one method in it called Main(). This is the method that C# uses as a starting point for execution. Every C# application has to have an entry point, and some types of C# projects often have more than one. For example, a DLL commonly has more than one entry point.

Hello, world Program

The canonical “Hello, world” program can be written in C# as follows:

The default file extension for C# programs is .cs, as in hello.cs. Such a program can be compiled with the command line directive
csc hello.cs which produces an executable program named hello.exe. The output of the program is:
Hello, world
Close examination of this program is illuminating:

  • The using System; directive references a namespace called System that is provided by the .NET runtime. This namespace contains the Console class referred to in the Main method. Namespaces provide a hierarchical means of organizing the elements of a class library. A ?using? directive enables unqualified use of the members of a namespace. The ?Hello, world? program uses Console.WriteLine as a shorthand for System.Console.WriteLine. What do these identifiers denote? System is a namespace, Console is a class defined in that namespace, and WriteLine is a static method defined on that class.
  • The Main function is a static member of the class Hello. Functions and variables are not supported at the global level; such elements are always contained within type declarations (e.g., class and struct declarations).
  • The ?Hello, world? output is produced through the use of a class library. C# does not itself provide a
    class library. Instead, C# uses a common class library that is also used by other languages such as Visual
    Basic and Visual C++.

For C and C++ developers, it is interesting to note a few things that do not appear in the ?Hello, world? program.

  • The program does not use either ?::? or ?->? operators. The ?::? is not an operator in C# at all, and the ?->? operator is used in only a small fraction of C# programs. C# programs use ?.? as a separator in compound names such as Console.WriteLine.
  • The program does not contain forward declarations. Forward declarations are never needed in C# programs, as declaration order is not significant.
  • The program does not use #include to import program text. Dependencies between programs are handled symbolically rather than with program text. This system eliminates barriers between programs written in different languages. For example, the Console class could be written in C# or in some other language.

Working with Parameters

It’s not uncommon for class constructors to accept input parameters that configure the class in some way. In fact, many classes provide several overrides that enable the developer to select the most convenient method for creating the class. For example, a constructor might provide overrides that accept no parameters, a string, and an integer for the same class. Some overrides enable the developer to choose which optional parameters to provide with the constructor call.