Complete C# Language Guide
1. Introduction to C#
C# is used for building a variety of applications on the .NET platform.
Example:
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, World!");
2. Variables and Data Types
Variables store data values.
Example:
int age = 25;
string name = "John";
bool isStudent = true;
3. Operators
Operators perform operations on variables and values.
Example:
int a = 10, b = 5;
int sum = a + b;
bool result = (a > b);
4. Conditional Statements
Conditional statements control the flow of the program.
Complete C# Language Guide
Example:
int age = 18;
if (age >= 18) {
Console.WriteLine("Adult");
} else {
Console.WriteLine("Minor");
5. Loops
Loops repeat code.
Example:
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
6. Arrays
Arrays hold multiple values of the same type.
Example:
int[] numbers = {1, 2, 3};
Console.WriteLine(numbers[0]); // Outputs 1
7. Methods
Methods are reusable blocks of code.
Example:
void Greet() {
Console.WriteLine("Hello");
}
Complete C# Language Guide
Greet();
8. Object-Oriented Programming (OOP)
OOP involves:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Example:
class Animal {
public void Speak() {
Console.WriteLine("Animal sound");
9. Classes and Objects
Classes define structure, objects are instances.
Example:
class Car {
public string color = "Red";
Car myCar = new Car();
Console.WriteLine(myCar.color);
10. Exception Handling
Use try-catch to handle errors.
Example:
Complete C# Language Guide
try {
int x = 10 / 0;
} catch (DivideByZeroException e) {
Console.WriteLine("Cannot divide by zero");
11. File Handling
Read and write files using System.IO.
Example:
File.WriteAllText("test.txt", "Hello");
string content = File.ReadAllText("test.txt");
12. Advanced Topics
Some advanced features:
Interface Example:
interface IShape {
void Draw();
Delegate Example:
delegate void MyDelegate(string msg);
LINQ Example:
int[] nums = {1, 2, 3};
var even = nums.Where(n => n % 2 == 0);