Comprehensive C# Course - Lecture Notes

Jul 21, 2024

Comprehensive C# Course - Lecture Notes

Introduction

  • This is a comprehensive, free full course on C#
  • Covers hundreds if not thousands of examples
  • No advertising during the course
  • All code is available on GitHub

Tools and Installation

  • Using Visual Studio Community
  • Download from Visual Studio
  • Works on both Windows and Mac
  • The interface in Visual Studio can be customized via Tools > Options. Increase text size under Environment and Text Editor.

Setting Up a New Project

  • Go to File > New > Project
  • Select Visual C# and choose Console App (.NET Core) or other desired template
  • Choose project location and name
  • Click Create

First C# Program

  • Writing a hello world program
  • Using namespaces (using System;) to import classes and functions
  • Defining a namespace and class
  • Storing and printing values in Main method

Basic C# Concepts

Variables

  • bool, int, long, decimal, double, and float data types
  • Example: bool canIVote = true; int age = 30;
  • Converting data types using parse and toString methods
  • Use of escape characters (\n, \t, \\, etc.)

Arithmetic Operations

  • Common arithmetic operations (+, -, *, /, %)
  • Increment (++) and Decrement (--) operators
  • Compound assignment operators (+=, -=, *=, /=, %=)

String Operations

  • String Methods: Length, Contains, IndexOf, Remove, Insert, Replace, etc.
  • StringBuilder for string manipulations without performance penalty
  • String interpolation with $ and {}

Control Flow

If-Else

  • Basic structure: if...else if...else
  • Use relational (>, <, >=, <=, ==, !=) and logical (&&, ||, !) operators
  • Example: Age-based school recommendations

Conditional (Ternary) Operator

  • Syntax: condition ? consequence : alternative
  • Example: string canDrive = age >= 16 ? "Yes" : "No";

Switch Case

  • Uses: For when a variable needs to be compared against several values
  • Basic format: switch(variable) { case value: //statements; break; ...}
  • Example: School stages based on age

Loops

For Loop

  • Basic format: for(initialization; condition; iteration) { //code }
  • Example: Print numbers, generate index-based outputs

While Loop

  • Runs as long as a condition is true
  • Example: Print odd numbers

Do-While Loop

  • Executes code block at least once before checking the condition
  • Example: Number guessing game

Exception Handling

  • Use try, catch, and finally blocks
  • Example: Divide-by-zero error handling

Functions

  • Avoid code duplication with functions
  • Basics: access specifier return_type function_name(parameters) { //code }
  • Example: Sum function returning integer or string based on parameters
  • Named Parameters: Call functions with parameters out of order by naming them
  • Method Overloading: Same function name, different parameters

Object-Oriented Programming

Classes and Objects

  • Define real-world entities as code objects
  • Example: Class Animal with attributes: name, sound, methods
  • Constructors: Initialize objects with default or specific values
  • Inheritance: Class Dog inherits Animal, add unique attributes

Properties and Encapsulation

  • Properties with { get; set; }
  • Encapsulate fields, provide controlled access

Interfaces

  • Define contracts for classes to implement specific methods
  • Example: IDrivable interface with methods Move, Stop

Delegates and Events

  • Delegates: Reference methods; Example: Arithmetic operations
  • Use delegate, lambda expressions
  • Example: public delegate void Arithmetic(int x, int y);

Collections and Generics

Arrays and Lists

  • Arrays: int[] array = new int[5];
  • ArrayList: Dynamically sized, can hold mixed data types
  • List<T>: Generic lists for specific data types

Dictionaries and Sets

  • Store key-value pairs; Example: Dictionary<int, string> dict
  • HashSet<T>: Unique collection of elements

Stacks and Queues

  • Stack: LIFO (Last In First Out)
  • Queue: FIFO (First In First Out)

LINQ (Language Integrated Query)

  • Query collections of data
  • Examples: from item in collection where condition select item
  • Lambda Expressions: Simplify syntax for operations

Multithreading and Concurrency

  • Create and manage multiple threads
  • Example: Printing numbers with delay using Thread.Sleep(1000);
  • Use lock for thread safety

Files and I/O Operations

  • Read/Write data to files
  • StreamReader and StreamWriter for handling text files
  • BinaryReader and BinaryWriter for binary files

Working with Databases

Setting up SQL Server

  • Installing SQL Server Developer
  • Configuration for proper setup

Connecting to SQL Databases in C#

  • Using Visual Studio: Configure data source, connection strings
  • SQL Commands: Perform CRUD operations (Create, Read, Update, Delete)

Entity Framework

  • ORM for database management
  • Designing models and context to interact with databases

Advanced Concepts

Serialization

  • Store object state in a stream; Example: Binary and XML serialization

Reflection

  • Inspecting and interacting with object metadata at runtime

Conclusion

  • By the end of this course, you should be among the most knowledgeable individuals in C# programming
  • All code is on GitHub for reference and practice