Introduction to C# (original) (raw)

Last Updated : 31 Jan, 2025

C# (C-sharp) is a modern, object-oriented programming language developed by Microsoft as part of the .NET framework. It was first released in 2000 and it has become one of the most widely used languages for building Windows applications, web services, and more. C# combines the power of C and C++ with the simplicity of languages like Java and Visual Basic.

History of C#

C# was developed by Anders Hejlsberg at Microsoft as part of the .NET initiative. The language was introduced as a part of the .NET Framework in 2000, aiming to provide a simple and modern language that would overcome the limitations of other languages like C++ and Visual Basic. C# was designed to be a safe, efficient, and easy-to-use language for building a wide range of applications.

C# has evolved with many updates and new features and including support for dynamic programming, asynchronous programming, and LINQ (Language Integrated Query). It is now a key language for building applications on Microsoft platforms such as Windows, Azure, and .NET Core.

Key Features of C#

**1. Object-Oriented Programming (OOP)

C# is an object-oriented programming language means it encourages the use of classes and objects. The core concepts of OOP in C# are:

**2. Simplicity

C# was designed to be simple and easy to use. Its syntax is clean, and many complex features found in other languages, such as pointers, are either minimized or avoided altogether. Like Java or C++, C# is similar but simpler.

**3. Robustness

C# is a robust language that ensures high reliability and easy debugging. Features like exception handling, garbage collection, and type safety contribute to its reliability allows developers to write secure and error-free code.

**4. Security

C# provides built-in security features like type safety, which prevents many types of errors and security vulnerabilities. The Common Language Runtime (CLR) checks type safety and makes sure that code does not access memory outside its boundaries.

**5. Portability

C# is part of the .NET ecosystem, which allows us to write code that can be executed on different platforms. With .NET Core (now .NET 5 and beyond), C# code can now be run on platforms like Windows, macOS, and Linux, making it highly portable.

**6. High Performance

C# code is compiled into intermediate language (IL) code, which is then executed by the CLR. The use of Just-In-Time (JIT) compilation allows C# programs to run efficiently, and optimizations in recent versions of the .NET framework have improved performance.

Execution of C# Code

The execution of a C# program involves three main steps:

**1. Creating the Program

C# programs are written using text editors or Integrated Development Environments (IDEs) like Visual Studio or Visual Studio Code. The source code is saved with a .cs extension.

**2. Compiling the Program

The C# compiler (csc) converts the source code into an Intermediate Language (IL). This IL is platform-independent and can be executed on any system that has the .NET runtime installed.

**Finding a Compiler:

**3. Running the Program

The .NET runtime (CLR) executes the compiled IL code. The CLR translates the IL code into native machine code, specific to the platform and architecture and it enables the program to run on different operating systems and devices.

**Example: A simple program to print Hello Geeks

C# `

// C# program to print Hello Geeks using System;

namespace HelloGeeksApp {
class HelloGeeks {
// Main function static void Main(string[] args) {

        // Printing Hello Geeks
        Console.WriteLine("Hello Geeks");

        Console.ReadKey();
    }
}

}

`

**Explanation: Comments are used for explaining code and are used in similar manner as in Java or C or C++. Compilers ignore the comment entries and does not execute them. Comments can be of single line or multiple lines. Single line Comments:

**Syntax:

// Single line comment

/* Multi line comments*/

Advantages of C#

Disadvantages of C#

**Applications