Certainly! Here is a 30-lesson course outline for learning to program in C# with code examples for each lesson. The course is structured to build foundational knowledge and progressively cover more advanced topics.
Course Outline: Learning C# Programming
Lesson 1: Introduction to C#
- Overview: Understanding what C# is, its history, and its usage in modern applications.
- Code Example:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
Lesson 2: Setting Up Your Development Environment
- Overview: Installing Visual Studio and setting up a C# project.
- Code Example: N/A (Setup Instructions)
Lesson 3: Basic Syntax and Data Types
- Overview: Introduction to variables, data types, and basic syntax.
- Code Example:
using System;
class Program
{
static void Main()
{
int number = 10;
double pi = 3.14;
string message = "Hello, C#";
bool isLearning = true;
Console.WriteLine(number);
Console.WriteLine(pi);
Console.WriteLine(message);
Console.WriteLine(isLearning);
}
}
Lesson 4: Operators in C#
- Overview: Arithmetic, comparison, and logical operators.
- Code Example:
using System;
class Program
{
static void Main()
{
int a = 10;
int b = 5;
Console.WriteLine("a + b = " + (a + b));
Console.WriteLine("a - b = " + (a - b));
Console.WriteLine("a * b = " + (a * b));
Console.WriteLine("a / b = " + (a / b));
Console.WriteLine("a % b = " + (a % b));
Console.WriteLine("a > b: " + (a > b));
Console.WriteLine("a < b: " + (a < b));
Console.WriteLine("a == b: " + (a == b));
bool result = (a > b) && (b > 0);
Console.WriteLine("Result: " + result);
}
}
Lesson 5: Conditional Statements (if
, else if
, else
)
- Overview: Using conditional statements to control the flow of your program.
- Code Example:
using System;
class Program
{
static void Main()
{
int number = 20;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
}
}
Lesson 6: Switch Statements
- Overview: Using
switch
statements for multi-way branching. - Code Example:
using System;
class Program
{
static void Main()
{
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Weekend");
break;
}
}
}
Lesson 7: Loops (for
, while
, do-while
)
- Overview: Using loops to repeat actions.
- Code Example:
using System;
class Program
{
static void Main()
{
// For loop
for (int i = 0; i < 5; i++)
{
Console.WriteLine("For loop iteration: " + i);
}
// While loop
int j = 0;
while (j < 5)
{
Console.WriteLine("While loop iteration: " + j);
j++;
}
// Do-while loop
int k = 0;
do
{
Console.WriteLine("Do-while loop iteration: " + k);
k++;
}
while (k < 5);
}
}
Lesson 8: Arrays
- Overview: Introduction to arrays, declaring, initializing, and accessing elements.
- Code Example:
using System;
class Program
{
static void Main()
{
int[] numbers = { 1, 2, 3, 4, 5 };
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}
}
}
Lesson 9: Multidimensional Arrays
- Overview: Working with two-dimensional arrays.
- Code Example:
using System;
class Program
{
static void Main()
{
int[,] matrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}
Lesson 10: Functions/Methods
- Overview: Defining and calling functions/methods.
- Code Example:
using System;
class Program
{
static void Main()
{
int result = Add(5, 3);
Console.WriteLine("Sum: " + result);
}
static int Add(int a, int b)
{
return a + b;
}
}
Lesson 11: Function Overloading
- Overview: Creating multiple methods with the same name but different parameters.
- Code Example:
using System;
class Program
{
static void Main()
{
Console.WriteLine(Add(5, 3)); // Calls Add(int, int)
Console.WriteLine(Add(2.5, 3.5)); // Calls Add(double, double)
}
static int Add(int a, int b)
{
return a + b;
}
static double Add(double a, double b)
{
return a + b;
}
}
Lesson 12: Parameters and Return Types
- Overview: Understanding parameter passing and return values.
- Code Example:
using System;
class Program
{
static void Main()
{
string message = Greet("Alice");
Console.WriteLine(message);
}
static string Greet(string name)
{
return "Hello, " + name + "!";
}
}
Lesson 13: Arrays of Objects
- Overview: Working with arrays that hold objects.
- Code Example:
using System;
class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
}
class Program
{
static void Main()
{
Person[] people = {
new Person("Alice"),
new Person("Bob"),
new Person("Charlie")
};
foreach (Person person in people)
{
Console.WriteLine(person.Name);
}
}
}
Lesson 14: Classes and Objects
- Overview: Understanding classes and creating objects.
- Code Example:
using System;
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public void DisplayInfo()
{
Console.WriteLine("Make: " + Make + ", Model: " + Model);
}
}
class Program
{
static void Main()
{
Car myCar = new Car { Make = "Toyota", Model = "Corolla" };
myCar.DisplayInfo();
}
}
Lesson 15: Constructors and Destructors
- Overview: Using constructors and destructors in classes.
- Code Example:
using System;
class Book
{
public string Title { get; set; }
// Constructor
public Book(string title)
{
Title = title;
}
// Destructor
~Book()
{
Console.WriteLine("Book object is being destroyed.");
}
}
class Program
{
static void Main()
{
Book myBook = new Book("C# Programming");
Console.WriteLine("Title: " + myBook.Title);
}
}
Lesson 16: Inheritance
- Overview: Understanding how to create derived classes.
- **
Code Example**:
using System;
class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog();
myDog.Eat();
myDog.Bark();
}
}
Lesson 17: Polymorphism
- Overview: Using method overriding and polymorphism.
- Code Example:
using System;
class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark");
}
}
class Program
{
static void Main()
{
Animal myAnimal = new Dog();
myAnimal.MakeSound(); // Outputs: Bark
}
}
Lesson 18: Interfaces
- Overview: Defining and implementing interfaces.
- Code Example:
using System;
interface IAnimal
{
void MakeSound();
}
class Cat : IAnimal
{
public void MakeSound()
{
Console.WriteLine("Meow");
}
}
class Program
{
static void Main()
{
IAnimal myCat = new Cat();
myCat.MakeSound(); // Outputs: Meow
}
}
Lesson 19: Exception Handling
- Overview: Using
try
,catch
,finally
for error handling. - Code Example:
using System;
class Program
{
static void Main()
{
try
{
int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[5]);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
finally
{
Console.WriteLine("Execution completed.");
}
}
}
Lesson 20: Collections: Lists
- Overview: Using the
List<T>
class for dynamic arrays. - Code Example:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
names.Add("David");
foreach (string name in names)
{
Console.WriteLine(name);
}
}
}
Lesson 21: Collections: Dictionaries
- Overview: Using the
Dictionary<TKey, TValue>
class for key-value pairs. - Code Example:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary<string, int> ages = new Dictionary<string, int>
{
{ "Alice", 30 },
{ "Bob", 25 },
{ "Charlie", 35 }
};
Console.WriteLine("Age of Bob: " + ages["Bob"]);
}
}
Lesson 22: Working with Files
- Overview: Reading from and writing to files using
System.IO
. - Code Example:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "example.txt";
// Writing to a file
File.WriteAllText(path, "Hello, File!");
// Reading from a file
string content = File.ReadAllText(path);
Console.WriteLine(content);
}
}
Lesson 23: LINQ Basics
- Overview: Introduction to Language Integrated Query (LINQ) for querying collections.
- Code Example:
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = from number in numbers
where number % 2 == 0
select number;
foreach (var number in evenNumbers)
{
Console.WriteLine(number);
}
}
}
Lesson 24: Asynchronous Programming with async
and await
- Overview: Understanding asynchronous programming with
async
andawait
. - Code Example:
using System;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
Console.WriteLine("Starting...");
await Task.Delay(2000); // Simulate a delay
Console.WriteLine("Finished!");
}
}
Lesson 25: Delegates
- Overview: Understanding delegates and their usage for method references.
- Code Example:
using System;
delegate void GreetDelegate(string name);
class Program
{
static void Main()
{
GreetDelegate greet = Greet;
greet("Alice");
}
static void Greet(string name)
{
Console.WriteLine("Hello, " + name);
}
}
Lesson 26: Events
- Overview: Using events to implement event-driven programming.
- Code Example:
using System;
class Program
{
public delegate void NotifyEventHandler(string message);
public static event NotifyEventHandler Notify;
static void Main()
{
Notify += DisplayMessage;
Notify("Event triggered!");
}
static void DisplayMessage(string message)
{
Console.WriteLine(message);
}
}
Lesson 27: Properties
- Overview: Using properties to encapsulate data within a class.
- Code Example:
using System;
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class Program
{
static void Main()
{
Person person = new Person();
person.Name = "Alice";
Console.WriteLine(person.Name);
}
}
Lesson 28: Indexers
- Overview: Using indexers to allow objects to be indexed like arrays.
- Code Example:
using System;
class StringCollection
{
private string[] items = new string[10];
public string this[int index]
{
get { return items[index]; }
set { items[index] = value; }
}
}
class Program
{
static void Main()
{
StringCollection collection = new StringCollection();
collection[0] = "Hello";
Console.WriteLine(collection[0]);
}
}
Lesson 29: Exception Handling Best Practices
- Overview: Advanced exception handling techniques and best practices.
- Code Example:
using System;
class Program
{
static void Main()
{
try
{
int[] numbers = { 1, 2, 3 };
int result = numbers[5];
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("An index was out of range: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
finally
{
Console.WriteLine("Clean up code here.");
}
}
}
Lesson 30: Unit Testing with NUnit
- Overview: Introduction to unit testing in C# using NUnit.
- Code Example:
// Install NUnit and NUnit3TestAdapter via NuGet
using NUnit.Framework;
[TestFixture]
public class MathTests
{
[Test]
public void Add_SimpleValues_ReturnsSum()
{
int result = Add(2, 3);
Assert.AreEqual(5, result);
}
public int Add(int a, int b)
{
return a + b;
}
}
This course covers fundamental concepts and practical aspects of C# programming, from basic syntax to advanced topics like asynchronous programming and unit testing. Each lesson is designed to build upon the previous one, providing a comprehensive learning experience.
0 Comments