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
C# TutorialC# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET FrameworkThe .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# .NET Framework (Basic Architecture and Component Stack)C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Hello WorldThe Hello World Program is the most basic program when we dive into a new programming language. This simply prints "Hello World!" on the console. In C#, a basic program consists of the following:A Namespace DeclarationClass Declaration & DefinitionClass Members(like variables, methods, etc.)Main
4 min read
Common Language Runtime (CLR) in C#The Common Language Runtime (CLR) is a component of the Microsoft .NET Framework that manages the execution of .NET applications. It is responsible for loading and executing the code written in various .NET programming languages, including C#, VB.NET, F#, and others.When a C# program is compiled, th
4 min read
Fundamentals
C# IdentifiersIn programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
C# Data TypesData types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# VariablesIn C#, variables are containers used to store data values during program execution. So basically, a Variable is a placeholder of the information which can be changed at runtime. And variables allows to Retrieve and Manipulate the stored information. In Brief Defination: When a user enters a new valu
4 min read
C# LiteralsIn C#, a literal is a fixed value used in a program. These values are directly written into the code and can be used by variables. A literal can be an integer, floating-point number, string, character, boolean, or even null. Example:// Here 100 is a constant/literal.int x = 100; Types of Literals in
5 min read
C# OperatorsIn C#, Operators are special types of symbols which perform operations on variables or values. It is a fundamental part of language which plays an important role in performing different mathematical operations. It takes one or more operands and performs operations to produce a result.Types of Operat
7 min read
C# KeywordsKeywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to be used as variable names or objects. Doing this will result in a compile-time error.Example:C#// C# Program to illustrate the
5 min read
Control Statements
C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)Decision Making in programming is similar to decision making in real life. In programming too, a certain block of code needs to be executed when some condition is fulfilled. A programming language uses control statements to control the flow of execution of program based on certain conditions. These
5 min read
C# Switch StatementIn C#, Switch statement is a multiway branch statement. It provides an efficient way to transfer the execution to different parts of a code based on the value of the expression. The switch expression is of integer type such as int, char, byte, or short, or of an enumeration type, or of string type.
4 min read
C# LoopsLooping in a programming language is a way to execute a statement or a set of statements multiple times, depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Types of Loops in C#Loops are mainly divided
4 min read
C# Jump Statements (Break, Continue, Goto, Return and Throw)In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.Types of Jump StatementsThere are mainly five keywords in th
4 min read
OOP Concepts
Methods
Arrays
C# ArraysAn array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
C# Jagged ArraysA jagged array is an array of arrays, where each element in the main array can have a different length. In simpler terms, a jagged array is an array whose elements are themselves arrays. These inner arrays can have different lengths. Can also be mixed with multidimensional arrays. The number of rows
4 min read
C# Array ClassArray class in C# is part of the System namespace and provides methods for creating, searching, and sorting arrays. The Array class is not part of the System.Collections namespace, but it is still considered as a collection because it is based on the IList interface. The Array class is the base clas
7 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to find the rank of an array in C#Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
2 min read
ArrayList
String
Tuple
Indexers