Book Store  
  Contact Us  
  Links  
  Site Map  
C# Tutorials - language basics

C# Programming Language Basics

 

By now you should have run the Hello World application.

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

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

Namespaces

All the .net programming languages can make use of namespaces. They are used to group together related items. For example the System namespace groups together such functions as being able to write information to the screen.

The using statement at the beginning of the program code tells the compiler you are going to make use of classes in the System namespace. Including the using statement allows us to write :

Console.WriteLine("Hello World");

Rather than the longer :

System.Console.WriteLine("Hello World");

This means that the code you write can be more concise and the compiler will work out which namespace the Console class lives in.

namespace HelloWorld
{
... ....
}

Namespaces help when you create a class with the same name as an existing class. Defining it as your own namespace means that it can exist in the same application even though the class name is used elsewhere.

You can create your own class libraries and of useful bits and pieces and pass them on to other programmers. They will then be able to make use of them in their own applications by adding a line similar to this at the top of their code :

using HelloWorld;

Note that we do not have to create namespaces around the class and the code will work without it.

Classes

Everything you create in C# exists in classes.

They group together variables and functions into reusable blocks of code.

public class HelloWorld
{
....
}

This class is called HelloWorld.

Application Start-up: Main()

There must be a function called Main() with global scope (available everywhere) which can be used to start the application. It is called automatically when the operating system starts the program.

public static void Main()
{
... ...
}

The C# language is case-sensitive. All keywords are lowercase, but all method names and class names are title case, meaning that each word has a capital letter.

All the code to start the application must go in the Main() method.

The System.Console Class

Text-based applications are known as console applications, and the System.Console class encapsulates all the basic functionality needed for displaying information in a Command Prompt window and for reading information from the keyboard.

Displaying information

The most basic requirement for a console application is that it should display text.

System.Console.WriteLine("Hello World!");

The WriteLine() method displays a string of text in the Command Prompt window, followed by a new line character. There is also a write() method which does the same thing but without the new line.

System.Console.Write("Hello World");

Defining Variables

The following example illustrates the use of defining variables :

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

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

            int Apples = 0;
            double PriceEach = 0.0;
            double TotalPrice = 0.0;

            //read in number of apples
            System.Console.Write("Apples  ");
            Apples = Convert.ToInt16(System.Console.ReadLine());

            //read in price of apples
            System.Console.Write("Price each  ");
            PriceEach = Convert.ToDouble(System.Console.ReadLine());

            //total price
            TotalPrice = Apples * PriceEach;

            System.Console.WriteLine("{0} apples cost {1} each ", Apples, PriceEach);

            System.Console.WriteLine("Total Price {0}", TotalPrice);

        }
    }
}

Variables allow you to store values in memory so you can perform calculations on them. That concept is the same in all programming languages. The example program calculates the total price for an unknown number of apples so we need to store the number that the user types in memory.

int Apples = 0;
double PriceEach = 0.0;
double TotalPrice = 0.0;