C# is a programming language developed by Microsoft. It is a fully object-oriented programming language like Java, and it’s the only component presented by -oriented language. C# is derived from C and C++ languages. It is suitable for developing web-based applications and supports the .NET framework, Microsoft’s development platform.
Though C# is the successor of languages like C and C++ with several similarities in comparison, it contains several unique features:
- Boolean Conditions
- Automatic Garbage Collections
- Assembly Versioning
- Properties and Events
- Standard Library
- Easy to use Generics
- Simple Multithreading
- LINQ and Lambda Expressions
- Integration with Windows
What is .NET Framework?
Any language can access the .NET framework as well as communicate with each other using .NET libraries. In addition, it is a component-based model in which a program is broken into numerous individual components where each one offers a particular service. This mechanism is called Microsoft Intermediate Language (MSIL). The most striking feature of .NET framework is the compiler inter-operability by compiling code into an IL that is compatible with other IL modules. Microsoft .NET software provides building, deploying and running web services and other console applications. It consists of three different technologies:
- Common Language Runtime
- Framework Base Classes
- User & Program Interfaces (ASP.NET and Winforms)
Common Language Runtime (CLR) provides runtime environments for languages including C#.
The CLR provides a number of services that include:
- Loading and execution of programs
- Memory isolation for applications
- Compilation of IL into native executable code.
Hello World with C#
A simple C# program has following parts:
- Namespace Declaration
- Class methods
- Class attributes
- Main method
- Statements & Expressions
static void Main()
/* it is Hello World in C# */
- The first line “using System”: In this example, ‘using’ is the keyword used to include the “System” namespace in the program. A program has many ‘using’ statements and is similar to import statement in java or “include” statement in c.
- Namespace declaration: A namespace is a collection of classes. For instance, HelloWorld namespace contains “Hello” class.
- Next is the declaration of the class. The “”Hello” class contains methods definitions and members that are used in the program. Classes can contain any number of functions but there should be only one Main method.
- Next line defines the Main method, which points the compiler to the entry point of the execution of program. All methods and function calls are made from Main method.
- Console.WriteLine() is the method of console class defined inside the System namespace. This causes the text written inside the quotes to get printed on the screen.
- Everything written between /*….*/ is ignored by the compiler and they are known as comments. It helps to improve the readability and helps the user understand the program by mentioning details of tasks at different steps in complex programs.
Points to Note:
- C# is case sensitive language
- All statements must end with semicolon
- The program execution starts at Main method
Compiling and Running The C# Program:
From Visual Studio
- Start visual studio
- From Menu bar choose new project and choose C# template
- Choose Console application. The new project appears in Solution Explorer.
- Write the code and press F5 to compile and run the code
From Command line:
- Write the code in any text editor and save the file with “.cs” extension.
- Open command prompt and move to the directory where the program was saved.
- Type “csc filename” and press enter to compile the code. The executable file will be generated if there are no errors found.
- Type the filename to run the program. The output will be visible on the screen
Example of Command Line Arguments
static void Main(string args)
/* it is Hello World in using Command Line Argument */
Console.Write(“Welcome to Command Line “);
Notice in this Main method declaration
public static void main(string args)
Main method is enclosed with the parameter “args”. The parameter is the array of strings also known as “String objects“. Any arguments given in the command line at the time of execution is passed to “args” as its elements. These elements are then accessed by using subscript args, args and so on.
If you provide three arguments at the time of execution for instance
The only difference between WriteLine() and Write() method is that the latter does not create a line break and therefore the next output will be printed on the same line.
Applications of C#
- Console application
- Windows application
- Developing windows Controls
- Developing ASP.NET projects
- Providing Web Services
- Developing .NET Component library
Framework Base Classes
.NET supplies a library of base classes that help you implement web applications quickly. You can use them by instantiating the program and invoking their methods. The namespace named System includes a lot of functionality.
User and Program Interfaces
.NET provides many tools for managing user applications
- Windows Forms
- Web Forms
- Console Application
- Web Services
- These tools help the user to develop web based and desktop based applications using a wide variety of languages.
Tools Required for C#
- .NET Framework
- Integrated Development Environment: Visual Studio, Visual C# 2010 Express, And Visual Web Developer.
- Windows Operating System: C# can only run on systems that have .NET framework installed.
One thing to note is that .NET framework only works for Windows environment and you cannot run the C# program without .NET framework. Though Mono an open source platform of .NET framework includes C# compiler, it has been developed so that it can run C# program on cross platforms, including Mac OS.