C# Class Notes
1. Introduction to C#
C# (C-Sharp): A modern, object-oriented programming language developed by Microsoft as part
of the .NET framework.
Key Features: Strong typing, automatic garbage collection, and support for both imperative and
declarative programming paradigms.
2. Basics of C# Syntax
Hello World Program:
C#
using System;
class Program
static void Main()
Console.WriteLine("Hello, World!");
AI-generated code. Review and use carefully. More info on FAQ.
Namespaces: Used to organize code into logical groups and prevent name conflicts.
C#
namespace MyNamespace
class MyClass
// Class members
}
AI-generated code. Review and use carefully. More info on FAQ.
3. Classes and Objects
Class: A blueprint for creating objects, defining properties, methods, and events.
C#
public class Car
// Fields
private string color;
private int speed;
// Constructor
public Car(string color, int speed)
this.color = color;
this.speed = speed;
// Methods
public void Accelerate(int increment)
speed += increment;
public void DisplayInfo()
Console.WriteLine($"Color: {color}, Speed: {speed}");
AI-generated code. Review and use carefully. More info on FAQ.
Object: An instance of a class.
C#
Car myCar = new Car("Red", 0);
myCar.Accelerate(10);
myCar.DisplayInfo();
AI-generated code. Review and use carefully. More info on FAQ.
4. Properties
Properties: Provide a flexible mechanism to read, write, or compute the values of private fields.
C#
public class Student
private int id;
public int ID
get { return id; }
set { id = value; }
AI-generated code. Review and use carefully. More info on FAQ.
5. Methods
Methods: Define the behavior of a class.
C#
public class Calculator
public int Add(int a, int b)
return a + b;
}
AI-generated code. Review and use carefully. More info on FAQ.
6. Inheritance
Inheritance: Allows a class to inherit members from another class.
C#
public class Animal
public void Eat()
Console.WriteLine("Eating...");
public class Dog : Animal
public void Bark()
Console.WriteLine("Barking...");
AI-generated code. Review and use carefully. More info on FAQ.
7. Polymorphism
Polymorphism: Allows methods to do different things based on the object it is acting upon.
C#
public class Shape
public virtual void Draw()
Console.WriteLine("Drawing a shape.");
}
}
public class Circle : Shape
public override void Draw()
Console.WriteLine("Drawing a circle.");
AI-generated code. Review and use carefully. More info on FAQ.
8. Interfaces
Interfaces: Define a contract that implementing classes must follow.
C#
public interface IMovable
void Move();
public class Car : IMovable
public void Move()
Console.WriteLine("Car is moving.");
AI-generated code. Review and use carefully. More info on FAQ.
9. Exception Handling
Exception Handling: Provides a way to handle runtime errors.
C#
try
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[5]);
catch (IndexOutOfRangeException ex)
Console.WriteLine("An error occurred: " + ex.Message);
AI-generated code. Review and use carefully. More info on FAQ.
10. Collections
Collections: Used to store groups of related objects.
o List: A dynamic array.
C#
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Add(6);
foreach (int number in numbers)
Console.WriteLine(number);
AI-generated code. Review and use carefully. More info on FAQ.
11. LINQ (Language Integrated Query)
LINQ: Provides a consistent way to query data from different sources.
C#
int[] numbers = { 1, 2, 3, 4, 5 };
var evenNumbers = from num in numbers
where num % 2 == 0
select num;
foreach (var num in evenNumbers)
Console.WriteLine(num);
AI-generated code. Review and use carefully. More info on FAQ.
12. Asynchronous Programming
Async and Await: Simplify asynchronous programming.
C#
public async Task<string> GetDataAsync()
await Task.Delay(1000); // Simulate a delay
return "Data retrieved";
public async void DisplayData()
string data = await GetDataAsync();
Console.WriteLine(data);
AI-generated code. Review and use carefully. More info on FAQ.
These notes provide a comprehensive overview of C#, suitable for a class discussion or study guide. If
you need more detailed explanations or specific examples, feel free to ask!