Name: C# (C-Sharp)
First Developed: 2000
Creator: Anders Hejlsberg at Microsoft
Type: Object-Oriented, General-purpose, High-level programming language
Platform: Part of the .NET ecosystem
Primary Use: Building Windows applications, Web applications (via ASP.NET), and mobile applications (via Xamarin)
Current Version: C# 11 (as of 2025)
Object-Oriented Programming (OOP):
C# is primarily object-oriented, focusing on concepts like classes, inheritance, polymorphism, abstraction, and encapsulation.
Code is organized into reusable objects, classes, and methods, making it easier to manage large and complex systems.
Type Safety:
C# is a statically typed language, meaning variables and their types are defined at compile time, reducing errors and improving code stability.
Type safety ensures that operations on data types are valid and prevents type-related errors.
Automatic Memory Management (Garbage Collection):
C# uses an automatic garbage collector (GC) to manage memory allocation and deallocation, preventing memory leaks and improving developer productivity.
Platform Independence (with .NET Core/.NET 5+):
While originally designed for Windows, C# is now cross-platform through .NET Core and .NET 5+, making it possible to run C# applications on Windows, macOS, and Linux.
Modern Syntax:
C# features a clean and modern syntax that is easy to read and write. It incorporates advanced features such as LINQ (Language Integrated Query), async/await for asynchronous programming, and pattern matching.
Language Interoperability:
C# is part of the .NET ecosystem, allowing it to interoperate with other .NET languages like Visual Basic.NET and F#.
Rich Standard Library:
C# comes with a comprehensive standard library (the .NET Base Class Library, or BCL), offering built-in functions for working with collections, file I/O, networking, database access, and more.
Multithreading and Asynchronous Programming:
C# supports asynchronous programming with async and await, allowing developers to create responsive applications. It also supports multithreading, enabling programs to run multiple operations concurrently.
Variables and Data Types:
C# supports several built-in data types such as int, double, float, char, string, bool, decimal, and custom types like structs and enums.
Syntax for declaring a variable:
int x = 5; // Declaring an integer variable
double y = 3.14; // Declaring a floating-point variable
string name = "John"; // Declaring a string variable
Functions (Methods):
C# code is organized into methods, with Main() being the entry point for console applications.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
Control Flow:
C# supports if, else, switch, for, foreach, while, and do-while loops.
int x = 10;
if (x > 5)
{
Console.WriteLine("x is greater than 5");
}
Arrays:
C# arrays are used to store multiple values of the same data type.
int[] arr = {1, 2, 3, 4, 5};
Console.WriteLine(arr[2]); // Prints 3
Classes and Objects:
C# is an object-oriented language, and objects are instances of classes.
class Person
{
public string Name;
public int Age;
public void Introduce()
{
Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old.");
}
}
// Creating an object
Person person1 = new Person();
person1.Name = "John";
person1.Age = 30;
person1.Introduce(); // Prints "Hi, I'm John and I'm 30 years old."
Inheritance:
Inheritance allows one class to inherit methods and properties from another.
class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
Dog dog = new Dog();
dog.Eat(); // Inherited method
dog.Bark(); // Dog-specific method
Polymorphism:
Polymorphism allows objects to be treated as instances of their parent class.
class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Some generic animal sound.");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark!");
}
}
Animal myDog = new Dog();
myDog.MakeSound(); // Outputs "Bark!" using overridden method
LINQ (Language Integrated Query):
LINQ allows you to query collections and databases using SQL-like syntax directly within C#.
int[] numbers = { 1, 2, 3, 4, 5 };
var evenNumbers = from num in numbers
where num % 2 == 0
select num;
foreach (var num in evenNumbers)
{
Console.WriteLine(num); // Prints 2, 4
}
Asynchronous Programming:
async and await keywords allow developers to write non-blocking code, improving application responsiveness.
async Task FetchDataAsync()
{
var data = await HttpClient.GetStringAsync("http://example.com");
Console.WriteLine(data);
}
Delegates and Events:
Delegates are references to methods, and events are used to provide notifications to other parts of the program.
public delegate void Notify(); // Delegate
public event Notify OnNotify; // Event
class Program
{
static void Main()
{
OnNotify += new Notify(SendNotification); // Register event
OnNotify(); // Trigger event
}
static void SendNotification()
{
Console.WriteLine("Notification sent!");
}
}
Properties:
Properties provide a way to access fields in a class without directly exposing them.
class Person
{
private string name;
public string Name // Property
{
get { return name; }
set { name = value; }
}
}
Exception Handling:
C# uses try, catch, and finally blocks to handle exceptions and clean up resources.
try
{
int result = 10 / 0; // Division by zero will throw exception
}
catch (DivideByZeroException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
Console.WriteLine("Finally block executed.");
}
Desktop Applications:
C# is widely used to develop Windows applications using frameworks like Windows Forms or WPF (Windows Presentation Foundation).
Web Applications:
Using ASP.NET (a framework built on C#), developers can build dynamic web applications and services.
Mobile Applications:
C# can be used to develop mobile applications through Xamarin, allowing cross-platform development for iOS and Android.
Game Development:
Unity, one of the most popular game engines, uses C# for scripting and game development.
Cloud Applications:
C# is commonly used for building cloud-based applications on platforms like Microsoft Azure.
Ease of Learning: C# has a clean and modern syntax, making it relatively easy for beginners to learn.
Cross-Platform Development: With .NET Core and .NET 5+, C# is now fully cross-platform, running on Windows, macOS, and Linux.
Rich Ecosystem: C# benefits from the extensive .NET ecosystem, offering libraries, frameworks, and tools for almost any type of development.
Strong Community Support: C# has a large, active community, making it easy to find resources, tutorials, and solutions to problems.
Runtime Dependencies: While C# is now cross-platform, it still requires the .NET runtime to execute, which may not always be ideal for lightweight applications.
Learning Curve for Advanced Features: While the basics of C# are easy to learn, advanced features like LINQ, delegates, and async programming can take time to master.
Less Low-Level Control: Compared to languages like C or C++, C# is more abstracted, making it less suitable for low-level systems programming.
C# is a versatile and powerful language, suitable for a wide range of applications, from desktop and web development to mobile and game programming. With its modern syntax, strong object-oriented principles, and rich library support, C# is a popular choice among developers, particularly in the Microsoft ecosystem. Its ability to run cross-platform through .NET Core makes it even more attractive for developers looking to build applications for different operating systems.