Saturday 28 January 2017

C# Programming Language

By,
Parikshit

C# Introduction:
C# is based on .NET framework .  Microsoft included it.  C# is designed to be a simple, object-oriented programming language, modern, general-purpose, borrowing key concepts from several other languages, most notably Java. C# can be run directly to machine code but it is used with combination of .NET framework. Hence there is machine requirement of .NET framework. .NET supports multiple languages. C# is sometimes referred to as THE .NET language, perhaps because it was designed together with the framework. C# is based on Object Oriented Programming hence did not support global functions or variables. Everything is placed in classes, even simple data types like int and string, which inherits from the System. Object class.
C# can be written in any simple text editor like windows notepad and after that can be compiled with C# command line compiler csc.exe. For C# programming you are required Visual C# Express from http://www.microsoft.com/express/download/. After installation you have to install it.

How to use C#:
If you have done programming in C it is similar to C. So you will find quite easier for programming in C#.
Start Visual C# Express, select File -> New project… From the project dialog, select the Console application. This is the most basic application type on a Windows system, but don't worry, we won't stay here for long. Once you click Ok, Visual C# Express creates a new project for you, including a file called program.cs.

Following is simple program in c#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args){}
        }
}

After writing the program save it and press F5 from your keyboard to compile and run the code. After doing this you will see only blank black screen appeared and gone with few seconds. Because we have not putted anything inside the code.  To see the output use following code containing Hello world as a message.

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, world!");
            Console.ReadLine();
        }
    }
}

Again save it and press F5 to execute the code. You will now see the message Hello World printed in above code.
The first line uses the Console class to output a line of text, and the second one reads a line of text from the console. Read? Why? Actually this is a bit of a trick, since without it, the application would just end and close the window with the output before anyone could see it.
The ReadLine command tells the application to wait for input from the user, and as you will notice, the console window now allows you to enter text. Press Enter to close it. Congratulations, you have just created your first C# application! Read on in the next chapter for even more information about what's actually going on.

No comments:

Post a Comment