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 ide, CodeChef 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 Express, Visual 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();
}
}
}
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.
Similar Reads
Introduction to .NET Framework
The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# | Generics - Introduction
Generic is a class which allows the user to define classes and methods with the placeholder. Generics were added to version 2.0 of the C# language. The basic idea behind using Generic is to allow type (Integer, String, ⦠etc and user-defined types) to be a parameter to methods, classes, and interfac
6 min read
What is Reflection in C#?
Reflection is the process of describing the metadata of types, methods and fields in a code. The namespace System.Reflection enables you to obtain data about the loaded assemblies, the elements within them like classes, methods and value types. Some of the commonly used classes of System.Reflection
4 min read
C# Constructors
Constructor is a special method of the class which gets automatically invoked whenever an instance of the class is created. Constructors in C# are fundamental components of object-oriented programming. Like methods, It contains the collection of instructions that are executed at the time of Object c
5 min read
C# List Implementation
In C#, a List is a generic collection used to store the elements or objects in the form of a list defined under System.Collection.Generic namespace. It provides the same functionality as ArrayList, the difference is a list is generic whereas ArrayList is a non-generic collection. It is dynamic means
7 min read
C# | Default Constructor
If you don't provide a constructor for your class, C# creates one by default that instantiates the object and sets member variables to the default values as listed in the Default Values Table. Constructor without any parameters is called a default constructor. In other words, this type of constructo
2 min read
Private Constructors in C#
Prerequisite: Constructors in C# Private Constructor is a special instance constructor present in C# language. Basically, private constructors are used in class that contains only static members. The private constructor is always declared by using a private keyword. Key PointsIt is the implementatio
3 min read
Introduction to C# Windows Forms Applications
Windows Forms (WinForms) is a GUI class library for building Windows desktop applications. Originally part of the .NET Framework, it is now supported in .NET Core, .NET 5+, .NET 8 (LTS), and .NET 9. Its main purpose is to provide an easier interface to develop applications for desktops, tablets, and
4 min read
How to create a StringCollection in C#
StringCollection() constructor is used to initializing a new instance of the StringCollection class. StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace
2 min read
C# Dictionary
Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
5 min read