💻

C# Programming Concepts and Practices

Dec 24, 2024

C# Programming Lecture Notes

Introduction

  • Overview of C# programming language.
  • Flexible language used for console apps, web services, games, etc.
  • Average salary for a C# developer: $63,000/year.

Getting Started with C#

IDE

  • Recommended IDE: Visual Studio Community.
  • Installation steps:
    • Download Visual Studio and follow installation procedures.
    • Select .NET desktop development package.
    • Choose color theme (dark preferred).

Creating a New Project

  • Select File -> New Project.
  • Choose C# Console Application.
  • Name project (e.g., "My First Program").
  • Customize font size in Visual Studio settings.

Basics of C# Programming

"Hello World" Program

  • Default program displays "Hello World".
  • Main method as the entry point for the program.
  • Compile and run by clicking the green play button.

Output Methods

  • Use Console.WriteLine() for output.
  • Change text output by editing contents within quotes.
  • Example:
    • Console.WriteLine("I like pizza");

Console Customization

  • Adjust console font and size via properties.

Variables in C#

Declaration and Initialization

  • Declare variables and assign values:
    • int x = 123;
  • Use descriptive variable names (e.g., int age = 21;).

Data Types

  • Common data types: int, double, bool, char, string.
  • Example of using bool:
    • bool isAlive = true;

Comments

  • Use single-line comments // and multi-line comments /*...*/.

Control Flow

If Statements

  • Check conditions using if-else statements: if (age >= 18) Console.WriteLine("You're signed up!"); else Console.WriteLine("Must be 18+");

Switch Statements

  • Efficient alternative to multiple else-if statements.
  • Example: switch (day) { case "Monday": Console.WriteLine("It's Monday"); break; }

Loops in C#

While Loops

  • Continue executing code while a condition is true: while (name == "") { Console.WriteLine("Enter your name:"); name = Console.ReadLine(); }

For Loops

  • Execute a block of code a finite number of times: for (int i = 0; i < 10; i++) { Console.WriteLine(i); }

Methods in C#

Defining Methods

  • Methods for reusable code: public void SayHello() { Console.WriteLine("Hello!"); }

Overloading Methods

  • Same name but different parameters.

Object-Oriented Concepts

Classes and Objects

  • Class as a blueprint for creating objects.
  • Objects as instances of a class.
  • Example: class Dog { public void Bark() { Console.WriteLine("Woof!"); } }

Inheritance

  • Child classes inherit fields and methods from parent classes.
  • Example: class Animal { ... } class Dog : Animal { ... }

Polymorphism

  • Objects can be treated as instances of their parent class.

Interfaces

  • Define what methods a class must implement.
  • Example: interface IFlyable { void Fly(); }

Generics

  • Create classes and methods that operate on any data type. public class GenericList<T> { ... }

Exception Handling

  • Use try-catch blocks to handle exceptions: try { // Code that may throw an exception } catch (SpecificException e) { // Handle exception }

Properties and Access Modifiers

  • Use properties with getters and setters for encapsulation: public int Speed { get; set; }

Auto-Implemented Properties

  • Shortcuts for properties without extra logic.

Enums

  • Define named constants: enum Days { Sunday, Monday, Tuesday }

Lists

  • Dynamically sized collections of objects: List<string> fruits = new List<string>();

Multi-threading

  • Create multiple threads to perform tasks concurrently: Thread thread1 = new Thread(new ThreadStart(MethodName)); thread1.Start();

Summary

  • Review of key concepts and tools for programming in C#.
  • Emphasis on exploration and practice.