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:

This is the simplest C# program. It prints "Hello, C#" to the console using Console.WriteLine().
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:

Three variables are declared: an integer, a string, and a boolean, each with a specific data type.
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:

Depending on the value of score, a different message is displayed using if-else logic.
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:

This for loop prints numbers from 1 to 5 by incrementing i.
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:

This method Greet takes a string parameter and prints a greeting message
Arrays

Arrays store multiple values of the same type.

Example:
string[] fruits = { "Apple", "Banana", "Orange" }; Console.WriteLine(fruits[1]);

Example Description:

An array of strings is declared. The second element "Banana" is accessed and printed.
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:

A class Car with a property Model is created. An object myCar is then used to access that property.
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:

The constructor accepts a name parameter and assigns it to the object’s Name property.
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:

The class Dog inherits from Animal and can use its method Speak().
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:

This code tries to parse an invalid string to an integer, which causes a FormatException. The error is caught and displayed.
You have completed the lesson . Add To Your Completed Course

You have a test for this course. Do you want to submit it to pass the course?