Complete C# Language Guide
1. Introduction to C#
C# (pronounced C-Sharp) is a modern, object-oriented programming language developed by Microsoft as
part of its .NET initiative. It is designed for building a wide range of applications including web, mobile,
desktop, and games. C# is type-safe, scalable, and supports modern programming features like garbage
collection, exception handling, and asynchronous programming.
Example:
using System;
class Program {
static void Main() {
[Link]("Welcome to C# Programming!");
2. Variables and Data Types
Variables are containers for storing data values. Each variable in C# must be declared with a data type.
Common Data Types:
- int: Stores integers (e.g., 1, 100, -10)
- float/double: Stores decimal numbers
- char: Stores a single character (e.g., 'A')
- string: Stores text (e.g., "Hello")
- bool: Stores true or false
Example:
int age = 30;
string name = "Alice";
bool isStudent = false;
3. Operators
Complete C# Language Guide
Operators are symbols used to perform operations on variables and values.
Types of Operators:
- Arithmetic: +, -, *, /, %
- Comparison: ==, !=, >, <, >=, <=
- Logical: &&, ||, !
- Assignment: =, +=, -=, *=
Example:
int a = 10, b = 5;
int sum = a + b;
bool isEqual = (a == b);
4. Conditional Statements
Conditional statements allow you to execute specific blocks of code based on certain conditions.
Types:
- if
- if-else
- else if
- switch
Example:
int number = 10;
if (number > 0) {
[Link]("Positive number");
} else {
[Link]("Non-positive number");
5. Loops
Complete C# Language Guide
Loops are used to execute a block of code repeatedly.
Types:
- for: loops with a known number of iterations
- while: loops while a condition is true
- do-while: loops at least once, then checks condition
Example:
for (int i = 1; i <= 5; i++) {
[Link](i);
6. Arrays
An array is a collection of elements of the same type stored in a contiguous memory location.
Example:
int[] numbers = {1, 2, 3, 4};
[Link](numbers[0]); // Outputs 1
You can also declare an array like:
int[] values = new int[5];
7. Methods
Methods are blocks of code that perform a specific task. They make code reusable and organized.
Syntax:
returnType MethodName(parameters) {
// code
Example:
Complete C# Language Guide
void Greet(string name) {
[Link]("Hello " + name);
Greet("John");
8. Object-Oriented Programming (OOP)
OOP is a programming paradigm that organizes code using objects and classes. Key principles:
- Encapsulation: Bundling data and methods
- Inheritance: Deriving new classes from existing ones
- Polymorphism: One interface, multiple implementations
- Abstraction: Hiding complexity using interfaces or abstract classes
Example:
class Animal {
public virtual void Speak() {
[Link]("Animal speaks");
class Dog : Animal {
public override void Speak() {
[Link]("Dog barks");
9. Classes and Objects
A class is a blueprint for creating objects. An object is an instance of a class.
Example:
class Car {
public string color = "Red";
Complete C# Language Guide
public void Drive() {
[Link]("Driving...");
Car myCar = new Car();
[Link]();
10. Exception Handling
Exception handling deals with errors that occur during program execution. C# uses try-catch-finally blocks.
Example:
try {
int result = 10 / 0;
} catch (DivideByZeroException e) {
[Link]("Error: " + [Link]);
11. File Handling
C# provides classes in the [Link] namespace to work with files.
Example:
[Link]("[Link]", "Hello");
string content = [Link]("[Link]");
[Link](content);
12. Advanced Concepts
- Interfaces: Define contracts
- Delegates: Point to methods
- Events: Notify when something happens
- LINQ: Query collections
Complete C# Language Guide
- async/await: Asynchronous programming
Interface Example:
interface IShape {
void Draw();
Delegate Example:
delegate void MyDelegate(string msg);
MyDelegate del = [Link];
del("Hello from delegate");