C# Basics – Study Notes
1. Introduction to C#
C# is a modern, object-oriented programming language by Microsoft.
Runs on the .NET platform (Common Language Runtime - CLR).
Compiles to Intermediate Language (IL).
Used for:
o Desktop apps
o Web apps (ASP.NET)
o Mobile apps (Xamarin)
o Games (Unity)
Features:
Object-oriented
Type-safe
Automatic garbage collection
Rich class libraries
2. Data Types
A. Value Types (Stored on Stack)
Type Example Description
int 4 bytes Integer
float 4 bytes Floating point
double 8 bytes Double precision float
char 2 bytes Unicode character
bool 1 bit true or false
B. Reference Types (Stored on Heap)
string
object
class
array
interface
3. Identifiers
Names for variables, classes, methods, etc.
Rules:
o Start with a letter or _
o Cannot use keywords
o Case-sensitive (age ≠ Age)
4. Variables and Constants
Variable:
int age = 25;
string name = "Alice";
Constant:
const double PI = 3.14;
Readonly:
readonly int id; // set in constructor
5. C# Statements
Control Flow:
if, else, switch
for, while, do-while, foreach
Jump:
break, continue, return, goto
Exception Handling:
try { ... }
catch (Exception ex) { ... }
finally { ... }
6. Object-Oriented Concepts (OOP)
Principle Meaning
Encapsulation Bundle data + behavior
Abstraction Hide internal details
Inheritance Reuse code across classes
Principle Meaning
Polymorphism Same method acts differently
7. Classes and Objects
Class:
class Person {
public string name;
public void Greet() {
Console.WriteLine("Hello!");
}
}
Object:
Person p = new Person();
p.name = "John";
p.Greet();
8. Arrays and Strings
Arrays:
int[] numbers = {1, 2, 3};
Strings:
Immutable
Common methods:
o .ToUpper(), .Length, .Substring()
string name = "Alice";
Console.WriteLine(name.ToUpper()); // "ALICE"
9. System.Collections
Non-Generic (not type-safe):
ArrayList
Stack
Queue
Hashtable
Generic Collections (type-safe):
List<int> nums = new List<int>();
Dictionary<int, string> dict = new Dictionary<int, string>();
10. Delegates and Events
Delegate:
delegate void MyDelegate(string message);
Event (built on delegate):
event MyDelegate OnMessage;
11. Indexers
Allow object indexing like an array:
class Sample {
private int[] data = new int[5];
public int this[int i] {
get => data[i];
set => data[i] = value;
}
}
12. Attributes
Add metadata to code.
Example:
[Obsolete("Use NewMethod instead")]
void OldMethod() { }
Common Attributes:
Obsolete
Serializable
DllImport
13. Versioning
Used in assemblies for tracking builds.
[assembly: AssemblyVersion("1.2.3.4")]
Part Meaning
Part Meaning
1 (Major) Breaking changes
2 (Minor) New features
3 (Build) Bug fixes
4 (Revision) Maintenance/patches
Quick Review Chart
Topic Purpose
Data Types Define size and type of values
Identifiers Naming variables, methods, etc.
Variables & Constants Store values (mutable and immutable)
Statements Control flow (if, loops, try-catch)
OOP Concepts Reusability and modular code
Classes & Objects Define and use entities
Arrays & Strings Manage sequences of data
Collections Manage groups of objects
Delegates & Events Callback mechanisms, messaging
Indexers Enable array-like access to objects
Attributes Add metadata for runtime or compile-time logic
Versioning Manage application releases and builds