KodeBLOG

Kode Blog - Inspiring And Empowering Developers.

Home About Us Courses Categories Blog Contact Us

C# Program Structure

Introduction

In this tutorial, we are going to look at the basic structure of a C# program. We will also cover the key concepts that you need to know about C# programs.

Topics to be covered

  • Program Structure
    • Properties
    • References
    • Program.cs
  • C# Program output types
    • Windows Application
    • Console Application
    • Class Library
  • Basic code structure of a C# program
  • Compiling and running C# Programs
    • Build Directory
    • Output Window
    • Error List Window

Program Structure

This tutorial assumes you are using Visual Studio as the IDE for developing C# applications. We will start by creating a new project.

  1. Open Visual Studio
  2. From the start page, select New Project. Alternatively, you can press Ctrl + Shift + N to create a new project.
  3. You will get the following New Project dialog window Tutorial Image
  4. Under installed templates, ensure that Windows under Visual C# node is selected
  5. Select Console Application as shown in the image above
  6. Enter the program name as shown in the image above. For this exercise, we will use CSharpProgram
  7. Click on OK button when you are done

By default, your program will have two directories and one file in the project explorer as shown in the image below

Tutorial Image

  • Properties - this directory contains AssemblyInfo.cs class. AssemblyInfo.cs contains important information such as;
    • Assembly Title - the title of the assembly
    • AssemblyDescription - the description of the assembly
    • AssemblyCompany - the name of the company that owns the product. This information shows in the control panel and all files associated with the installed product.
    • AssemplyProduct - the name of the product
    • AssemblyCopyright - Copyright notices
    • AssemblyTrademark - Program Trademark name
    • AssemblyVersion - Assembly version number
  • References - this directory contains the names of the libraries that the project is referencing
  • Program.cs this class is automatically created when you create a new project. It contains the Main method. The Main method is the method that is executed when you run a C# project.

Setting Visual Studio C# Project Properties

  1. Click on Project Menu
  2. Select CSharpProgram Properties... Note: If your project is named Payroll, then you will see Payroll Properties... Tutorial Image
  3. (1) The default Assembly Name is the name that you give the project
  4. (2) The default Namespace is the name that you give the project
  5. (3) Target frameworks shows the version of the .NET framework that your application will target. You can change it if you want.
  6. (4) Output type specifies the type of application that you are building. Click on the drop down box to see more options
    1. Windows Application - these are applications that a graphical user interface
    2. Console Application - these are applications that run from the command prompt
    3. Class Library - "applications" without any GUI. These are useful if you want to build business logic and compile it into a dll file. Other developers can reference the library and start using it without having access to the underlying source code.
  7. (5) Startup object - by default, this property says (Not set). It runs Program.cs static Main method. If you have created a custom class with a static Main method, then you can specify it here.
  8. (6) Assembly information - this is used to set information relating to the product and the company.
  9. (7) Icon and manifest - this is used to set the application icon that shows on the desktop,start menu etc. when you install the application

Setting Assembly Information

Click on Assembly Information button

Tutorial Image

The above properties are self explanatory. Enter the values as shown in the image above. Feel free to enter values of your choice if you want to. Click on OK button when you are done

Basic code structure of a C# program

Double click on Program.cs in the Solution explorer to open it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

HERE,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

The above lines import the System namespace and other sub namespaces of system. We will talk more about namespaces in the next tutorial.

namespace CSharpProgram
{
    ...
}

The above code uses the namespace keyword to define a namespace CSharpProgram

class Program
{
    ...
}

The above code uses the class keyword to define a class Program.

static void Main(string[] args)
{

}

The above code defines a static method Main. This is the method that the application will execute when it is run for the first time.

The method is static because C# has to access it without creating an instance of the class that contains the Main method

C# Hello World!

In the tradition of learning any computer programming language, we will write code that displays Hello World in the console window.

Add the following code inside the body of the Main method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadLine();
        }
    }
}

HERE,

Console.WriteLine("Hello World!");

The above code executes the WriteLine method of the Console object. We passed "Hello World!" as a parameter to WriteLine method.

Console.ReadLine();

The above code is a hack that prevents the Console window from closing after it has written Hello World!. If you don't include the ReadLine method, the console window will open and close immediately.

C# Application Requirements! A C# application must at-least import the System namespace, define it's own namespace and define a static Main method

Compiling and running C# Programs

Before we build our application, let's first display the output window in the IDE. The output window will enable us to view detailed information about the compiling.

Output Window

  1. Click on View Menu
  2. Select Output. Alternatively, you can press Ctrl + W, O
  3. Build is the term that is used to describe the process of compiling an application. Let's now build our application. Press F6 key on the keyboard. You will get the following results.

Tutorial Image

If something goes wrong during the compilation process, you will see detailed messages displayed in the output window

Let's now run the application. Press F5 key on the keyboard. You will get the following results

Tutorial Image

Build Directory

By default, Visual Studio builds the application file to bin directory inside the project directory. The bin directory contains a Debug directory. All of the build files related to the application will be in this directory

Error List

Another useful window that you need to know about is the Error List. It shows you information such as;

  1. Errors
  2. Warnings
  3. Messages

Press Ctrl + W, E to display the error list window at the bottom of the IDE. If an error that the compiler can detect occurs, it will be displayed in the error list. Let's illustrate this with an example. In our example, Replace Console with Consolex as shown below

Console.WriteLine("Hello World!");
Consolex.ReadLine();

You will the following results

Tutorial Image

If you double click on the Error message in the error list window, the IDE will open the file with the error and take you to the line that caused the error.

Summary

  • Developer tools increase developer productivity
  • IDE tools help write code better and faster
  • Unit testing tools help automate system tests
  • Database abstraction tools help improve the performance for database powered applications
  • Version control tools help manage the changes made to source code
  • Build tools are useful for compiling, packaging and deploying software
  • Other productivity tools include those for coding standards, performance testing and project management

Tutorial History

Tutorial version 1: Date Published 2015-08-21