Lesson 1.4

C# Basic Programming Examples


This chapter includes some basic programming examples with code explanation. It is mandatory to you to study this chapter completely and use examples in your PC. Don’t think much and just run the code and see output. Try to write similar program which display other output than examples.

Qu. 1: Write a program which display following message

Hi, I am C# Programmer and Learning Programming from CompileCode.net
Press Enter to EXIT

Solution

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

namespace DisplayMessage
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hi, I am C# Programmer and Learning Programming from CompileCode.net");
            Console.WriteLine("\n Press Enter to EXIT");
            Console.ReadLine();
        }
    }
}

Press F5 to run the program

OutputOpen File

Explanation

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

These are namespaces. A namespace is a collection of classes. It is added to a program with "using" Keyword.
For example

using System;

After adding namespace in a program, we can use its classes in our program. For example
Console is the class of namespace System and we use this class for printing information on screen.

Console.WriteLine("Hello Programmer")

Console.WriteLine() function is used for printing information on screen.

Console.ReadLine()

Console.ReadLine() function is used for accepting input from the user

Qu. 2: Write a program for adding two number and display addition as a result.

Solution

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

namespace DisplayMessage
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(5 + 4);
            Console.ReadLine();
        }
    }
}

Press F5 to run the program

OutputOpen File

In the next chapter, you will get some challenges. Actually it is exercises based on these examples. It is strongly recommended you to solve the problems using programming techniques and post your answer in comments. If you honestly go through all the examples and exercises, no doubt you are going to be a good programmer in next few days.

BackNext