C# Basics
C# = modern, object-oriented language for .NET
What is C#?
"Microsoft's object-oriented language"
C# is a modern, object-oriented programming language developed by Microsoft for the .NET platform.
"C# is a modern, strongly-typed, object-oriented programming language developed by Microsoft, primarily used for building .NET applications."
Key Characteristics
Object-oriented · Strongly typed · Garbage collected
- Object-oriented: Classes, inheritance, polymorphism
- Strongly typed: Type safety at compile time
- Garbage collected: Automatic memory management
- Modern features: LINQ, async/await, generics
"C# is object-oriented, strongly-typed, garbage-collected, and includes modern features like LINQ and async/await."
Data Types
Value types · Reference types
Value types (stack):
int,double,bool,charstruct,enum
Reference types (heap):
class,interface,delegatestring,array,object
"C# has value types (stored on stack) and reference types (stored on heap), with different memory behavior."
Classes and Objects
Class = blueprint, Object = instance
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void Greet()
{
Console.WriteLine($"Hello, {Name}");
}
}
Person person = new Person();
person.Name = "John";
"Classes define blueprints for objects, which are instances created with the 'new' keyword."
Properties
Auto-properties · Get/Set
// Auto-property
public string Name { get; set; }
// With backing field
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
// Read-only
public int Id { get; }
"Properties provide controlled access to class fields, with auto-properties, get/set accessors, and read-only options."
Methods
Return type · Parameters · Overloading
// Method
public int Add(int a, int b)
{
return a + b;
}
// Method overloading
public int Add(int a, int b, int c)
{
return a + b + c;
}
// Void method
public void Print(string message)
{
Console.WriteLine(message);
}
"Methods define behavior, support overloading (same name, different parameters), and can return values or be void."
Inheritance
Base class · Derived class
class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Sound");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Woof");
}
}
"Inheritance allows classes to inherit from base classes, with virtual/override for polymorphism."
Interfaces
Contract without implementation
interface IShape
{
double CalculateArea();
}
class Circle : IShape
{
public double CalculateArea()
{
return Math.PI * radius * radius;
}
}
"Interfaces define contracts that classes implement, enabling polymorphism and loose coupling."
9️⃣ Generics
Type parameters for reusability
class List<T>
{
private T[] items;
public void Add(T item) { }
}
List<int> numbers = new List<int>();
List<string> names = new List<string>();
"Generics enable type-safe, reusable code by using type parameters instead of specific types."
LINQ
Language Integrated Query
var numbers = new[] { 1, 2, 3, 4, 5 };
var evens = numbers.Where(n => n % 2 == 0);
var sum = numbers.Sum();
"LINQ provides query capabilities integrated into C#, enabling powerful data manipulation with a SQL-like syntax."
Async/Await
Asynchronous programming
async Task<string> FetchDataAsync()
{
var data = await httpClient.GetStringAsync(url);
return data;
}
"Async/await enables asynchronous programming, allowing non-blocking I/O operations and better performance."
Exception Handling
try · catch · finally
try
{
// Code that might throw
}
catch (Exception ex)
{
// Handle exception
}
finally
{
// Always executes
}
"Exception handling uses try/catch/finally blocks to handle errors gracefully and ensure cleanup."
"C# is a modern, object-oriented, strongly-typed language for .NET. It supports value and reference types, classes with inheritance, interfaces for contracts, generics for reusability, LINQ for queries, and async/await for asynchronous programming. C# is garbage-collected and provides exception handling with try/catch/finally."
🧠 Ultra-Short Cheat Sheet
Object-oriented
Strongly typed
Value types vs Reference types
Classes, inheritance, interfaces
Properties, methods
Generics
LINQ
Async/await
Exception handling