Open In App

Introduction to C#

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  • Abstraction: Hiding the complex implementation details and exposing only the essential features.
  • Encapsulation: Bundling the data and the methods that operate on that data within a class.
  • Inheritance: Allowing one class to inherit the properties and methods of another class.
  • Polymorphism: Enabling objects to take many forms and allows methods to behave differently depending on the object calling them.

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: 

  • There are various online IDEs such as GeeksforGeeks ideCodeChef ide etc. which can be used to run C# programs without installing. 
  • Windows: Since the C# is developed within .Net framework initiative by Microsoft, it provide various IDEs to run C# programs: Microsoft Visual Studio, Visual Studio ExpressVisual Web Developer 
  • Linux: Mono can be used to run C# programs on Linux. 
  • Programming in C#: Since the C# is a lot similar to other widely used languages syntactically, it is easier to code and learn in C#. Programs can be written in C# in any of the widely used text editors like Notepad++, gedit, etc. or on any of the compilers. After writing the program save the file with the extension .cs. 

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();
        }
    }
}

Output
Hello Geeks

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*/

  • using System: It includes the System namespace for basic functions.
  • namespace HelloGeeksApp: This groups related classes.
  • class HelloGeeks: It defines the program, with a Main method.
  • static void Main(): This is the entry point, static allows direct access, void means no return.
  • Console.WriteLine(“Hello Geeks”); It prints text to the console.
  • Console.ReadKey(): This pauses program (used in Visual Studio).
  • Semicolons: C# is case-sensitive, end statements with ;.

Advantages of C#

  • C# is very effective in managing the system. All garbage is collected automatically in C#.
  • There is no problem with memory leaks in C# due to the backup of high memory.
  • The cost of maintenance is low and is safe to run compared to other languages.
  • The C# code is collected for an intermediate language (generally (.NET) intermediate language) which is a standard language, despite the target operating system and architecture independently.
  • Easy and easy to learn: C# is designed as an easy to learn, especially for a programmer who is familiar with languages ​​such as Java and C ++. It has a clear syntax, which makes the code easy to read and write.
  • Large standard library: C# has a large standard library that includes a wide range of completed classrooms and features. This makes it easy for developers to do normal features without writing much customized code.
  • Strongly written: C# is a strongly written language, which means that data types are checked on the collection. This helps to reduce errors and improve the reliability of the code.

Disadvantages of C#

  • C# is less flexible because it depends a lot on .net Framework.
  • C# moves slowly and the program must be gathered every time a change is made.
  • Limited to Microsoft platforms: Although C# can be used to develop applications across platforms, it is still mainly related to the Microsoft platforms, which limits the use in some contexts.
  • Garbage collection: C# uses automatic waste collection to handle memory, which can cause display problems in some cases.
  • Learning state for advanced concepts: While C# is easy to learn for basic programming concepts, it can be challenging to master some more advanced concepts, such as people such as persons or parallel processing.
  • Limited support for functional programming: While C# supports some functional programming features, it is primarily an object -oriented language and may not be the best option for developers who prefer a functional programming style.

Applications

  • C# is widely used to develop desktop applications, web applications and web services.
  • It is used in large applications of Microsoft.
  • C# is also used in the development of sports in unity.
  • C# can be used to develop machine learning programs by using frames such as ml.net. Ml.net C# provides equipment for training and distribution to machine learning models in applications.
  • C# can be used to develop an IoT application using .Net IoT libraries. These applications can be run on devices such as Raspberry Pie and Arduino.
  • C# can be used to create a database application using ado.net or entity. These applications can be connected to different database systems, such as Microsoft SQL servers, Oracle and MySQL.
  • C# can be used to develop mobile applications across platforms by using frames like xamarin and .net maui. These applications can be run on Android, iOS and Windows devices.


Next Article
Article Tags :

Similar Reads