Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Major features of C# programming
C# is a modern, general-purpose, object-oriented programming language developed by Microsoft. C# is designed for Common Language Infrastructure (CLI), which consists of the executable code and runtime environment that allows the use of various high-level languages on different computer platforms and architectures.
C# combines the power of C++ with the simplicity of Visual Basic, making it an ideal choice for developing a wide range of applications from web services to desktop applications.
Key Features of C#
Object-Oriented Programming
C# is a fully object-oriented language that supports encapsulation, inheritance, and polymorphism. Everything in C# is treated as an object, making code organization intuitive and maintainable.
Example
using System;
public class Vehicle {
protected string brand;
public Vehicle(string brand) {
this.brand = brand;
}
public virtual void Start() {
Console.WriteLine(brand + " vehicle is starting...");
}
}
public class Car : Vehicle {
public Car(string brand) : base(brand) { }
public override void Start() {
Console.WriteLine(brand + " car engine started!");
}
}
public class Program {
public static void Main() {
Vehicle vehicle = new Car("Toyota");
vehicle.Start();
}
}
The output of the above code is −
Toyota car engine started!
Automatic Garbage Collection
C# provides automatic memory management through garbage collection. The runtime automatically deallocates memory for objects that are no longer referenced, preventing memory leaks.
Example
using System;
public class Person {
public string Name { get; set; }
public Person(string name) {
Name = name;
Console.WriteLine("Person " + name + " created");
}
~Person() {
Console.WriteLine("Person " + Name + " destroyed by garbage collector");
}
}
public class Program {
public static void Main() {
Person p1 = new Person("Alice");
Person p2 = new Person("Bob");
p1 = null; // Remove reference
p2 = null; // Remove reference
GC.Collect(); // Force garbage collection
GC.WaitForPendingFinalizers();
Console.WriteLine("Program completed");
}
}
The output of the above code is −
Person Alice created Person Bob created Person Bob destroyed by garbage collector Person Alice destroyed by garbage collector Program completed
Properties and Events
C# provides properties as a way to expose private fields with controlled access, and events for implementing the observer pattern efficiently.
Example
using System;
public class BankAccount {
private decimal balance;
public decimal Balance {
get { return balance; }
set {
if (value >= 0) {
balance = value;
OnBalanceChanged();
}
}
}
public event Action BalanceChanged;
protected virtual void OnBalanceChanged() {
BalanceChanged?.Invoke();
}
}
public class Program {
public static void Main() {
BankAccount account = new BankAccount();
account.BalanceChanged += () => Console.WriteLine("Balance changed to: $" + account.Balance);
account.Balance = 1000;
account.Balance = 1500;
}
}
The output of the above code is −
Balance changed to: $1000 Balance changed to: $1500
LINQ and Lambda Expressions
Language Integrated Query (LINQ) allows querying data using a SQL-like syntax, while lambda expressions provide a concise way to write anonymous functions.
Example
using System;
using System.Linq;
public class Program {
public static void Main() {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evenNumbers = numbers
.Where(n => n % 2 == 0)
.Select(n => n * n)
.ToArray();
Console.WriteLine("Squares of even numbers:");
foreach (int num in evenNumbers) {
Console.WriteLine(num);
}
int sum = numbers.Where(n => n > 5).Sum();
Console.WriteLine("Sum of numbers greater than 5: " + sum);
}
}
The output of the above code is −
Squares of even numbers: 4 16 36 64 100 Sum of numbers greater than 5: 30
Complete Feature Comparison
| Feature | Description | Benefit |
|---|---|---|
| Object-Oriented | Supports classes, inheritance, polymorphism | Code reusability and organization |
| Type Safety | Strong typing prevents runtime errors | Fewer bugs and better performance |
| Garbage Collection | Automatic memory management | No memory leaks, easier development |
| Properties & Events | Controlled access to data and notifications | Better encapsulation and loose coupling |
| Generics | Type-safe collections and methods | Reusable code without performance loss |
| LINQ | Query data using SQL-like syntax | Simplified data manipulation |
| Multithreading | Built-in support for concurrent programming | Better application performance |
Conclusion
C# offers a comprehensive set of features that make it ideal for modern application development. Its combination of object-oriented design, automatic memory management, rich standard library, and advanced features like LINQ and generics provides developers with powerful tools for building robust, maintainable applications across various platforms.
