Console Application


We will start our console application with the popular “Hello World” because that is always the first example in any programming language. Please pardon me; I will like to quickly introduce two keywords in C#. They are;
        1.   Console.ReadlLine(): used to feed data into the computer
        2.   Console.WriteLine(): used to output result or display text on the screen.
Now let us proceed to write the simple and common “Hello World” program

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");
        }
    }
}
Waoooooooh, welcome to your first C# program. I know that a lot of people will be discouraged because the code is too much. You shouldn’t be. The truth is that if you are using the latter of visual studio (2005 till date), most of the code is already written for you. All you need to do is to go to last of { } and add your code.
Some persons may ask, how can we get to the coding environment of console application of visual C#? Follow the steps below.
Steps to enter the coding environment of visual C# console application:
  1. Click on file
  2. Point to new 
  3.  Click on projects 
  4. Select console application

    5. Change the application name (you may decide to use the default name
        6. Click ok

That is the coding environment you are looking at.

Start your coding in-between the last { }, e.g.
static void Main(string[] args)
        {

            Console.WriteLine("Hello World");
        }
How do I see the output of my program. Follow the steps below.
How to run console application:
       1.   On the display menu, click on “Build”.
       2.   Click “Build helloworld” (the name you gave to your application). If there is no error in your application, the application will build successfully.
       3.   On the display menu again, click on “Debug”.
       4.   Click on “Start without Debugging”.
You will see a black window which is referred to as the DOS prompt or DOS window showing “Hello World”.
We will perform series of calculations as we move on. But before I continue let me introduce you to another keyword in C#. As a programmer, you can write plenty of code and later forget in future what those codes do. To avoid that, you need comment. Next we talk about comments.


Our next example is to write some programs that does mathematical computations. But before that can be effectively we have to talk about variables and constants.
What is a variable?
A variable is a storage location in your computer memory where data can be stored and retrieved.
A variable name can be any combination of letters, letters and numbers, etc, but cannot have spaces e.g. fashion90, car, la2k, etc. Do not begin variable name with number. Like 5kay, 100weo, are all wrong variable names. And remember that a variable name does not allow spaces instead use underscore sign (_).
The major types of variable types used in C# are:
1.   Int: These includes numbers without decimal part.
2.   Double: these includes numbers with decimal part.
3.   Float: same with double.
4.   String: used to display strings of characters.
5.   Char: same as string.
The various types in C# are enormous but we are going to discuss the few above.
What are constants?
Unlike variables constants are data storage location. As the name implies constants do not change.
Now let us demonstrate the above concepts with programs that multiply two numbers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

            double num1, num2, result;
            Console.WriteLine("Enter the value for num1");
            num1 = Convert.ToDouble(Console.ReadLine()); // assigning value to num1
            Console.WriteLine("Enter the value for num2");
            num2 = Convert.ToDouble(Console.ReadLine());// assigning value to num1
            result = num1 * num2; //mutiply two numbers
            Console.WriteLine("Result=" + result);
        }
    }
}


The program above shows the use of variables. You will notice that the user can input any value for num1 and num2. The result will always be different.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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


            double num1, num2, result;
           
            num1 = 3; // assigning value to num1
           
            num2 = 4;// assigning value to num1
            result = num1 * num2; //mutiply two numbers
            Console.WriteLine("Result=" + result);
        }
    }
}

The program shows the use of constant. Like I said earlier on that constant do not change. You will notice that when you run the program several time, it will always display the same result (12);

Try this:
using System;

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

namespace myfirstprogram
{
    class Program
    {
        static void Main(string[] args)
        {
            string a;
            Console.WriteLine("what is your name?");
            a=Convert.ToString(Console.ReadLine());
            Console.WriteLine("Hello" + a);
           

           
        }
    }
}

Study the program above and tell me what it does. You are free to copy the code into your C# environment.

No comments :

Post a Comment