C#
Object-oriented programming language developed by Microsoft, mainly for Windows applications
Lessons
- Introduction to C#
- Variables and Data Types
- Conditional Statements (if, else)
- Loops (for, while, foreach)
- Methods (Functions)
- Arrays
- Classes and Objects
- Constructors
- Inheritance
- Exception Handling (try-catch)
Introduction to C#
C# is an object-oriented programming language developed by Microsoft. It runs on the .NET platform and is used for desktop, web, and game development.
Example:
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, C#");
}
}
Example Description:
Variables and Data Types
C# is statically typed. You must declare the data type of each variable like int, string, bool, etc.
Example:
int age = 25;
string name = "Ali";
bool isStudent = true;
Example Description:
Conditional Statements (if, else)
Used to make decisions in your program.
Example:
int score = 85;
if (score >= 90)
Console.WriteLine("Excellent");
else if (score >= 70)
Console.WriteLine("Good");
else
Console.WriteLine("Needs improvement");
Example Description:
Loops (for, while, foreach)
Loops allow you to repeat actions multiple times.
Example:
for (int i = 1; i <= 5; i++) {
Console.WriteLine("Number: " + i);
}
Example Description:
Methods (Functions)
Methods are blocks of code that perform a specific task and can be reused.
Example:
static void Greet(string name) {
Console.WriteLine("Hello, " + name);
}
Example Description:
Arrays
Arrays store multiple values of the same type.
Example:
string[] fruits = { "Apple", "Banana", "Orange" };
Console.WriteLine(fruits[1]);
Example Description:
Classes and Objects
C# is object-oriented. A class is a blueprint; an object is an instance of that blueprint.
Example:
class Car {
public string Model = "Toyota";
}
Car myCar = new Car();
Console.WriteLine(myCar.Model);
Example Description:
Constructors
Constructors initialize objects when they are created.
Example:
class Person {
public string Name;
public Person(string name) {
Name = name;
}
}
Person p = new Person("Sara");
Console.WriteLine(p.Name);
Example Description:
Inheritance
Inheritance allows a class to inherit members (fields, methods) from another class.
Example:
class Animal {
public void Speak() {
Console.WriteLine("Animal sound");
}
}
class Dog : Animal {}
Dog d = new Dog();
d.Speak();
Example Description:
Exception Handling (try-catch)
Used to handle errors gracefully.
Example:
try {
int x = int.Parse("not a number");
} catch (FormatException e) {
Console.WriteLine("Error: " + e.Message);
}
Example Description: