💻

Lecture Notes on C# Basics

Jun 23, 2024

Lecture Notes on C# Basics

Introduction

  • Lecturer: Mike
  • Course: Introduction to C#
  • Overview: Learn the basics of C# including installation, programming concepts, data structures, and advanced topics such as object-oriented programming.
  • Applications: Extensive use in various software applications worldwide.

Getting Started

Installation and Setup

  • Tools Required: Visual Studio Community
  • Steps:
    1. Download Visual Studio Community from visualstudio.com
    2. Install dotnet desktop development with size around 3GB
    3. Launch Visual Studio
    4. Choose settings and start a new console application project in C#
  • First Program: Basic template provided by Visual Studio (program.cs)

Basics of C# Programming

Printing to Console

  • Example Code: Console.WriteLine("Hello World"); Console.ReadLine(); // To keep the console window open

Drawing Shapes

  • Print shapes using multiple WriteLine statements to draw e.g., a triangle.

Variables and Data Types

  • String: Represent text values
  • Character: Single character
  • Integer (int): Whole numbers
  • Double: Decimal numbers
  • Boolean (bool): True or false values

Working with Strings

  • Concatenation: Using + to combine strings
  • **Useful Methods: **
    • .Length: Get length of a string
    • .ToUpper(): Convert to uppercase
    • .Contains(): Check if substring is present
    • .Substring(startIndex, length): Extract part of a string

Numbers and Math Operations

  • Basic operations: +, -, *, /, %
  • Useful Methods from Math Library: .Abs(), .Pow(), .Sqrt(), .Max(), .Min(), .Round()*

User Input

  • Usage: Console.ReadLine()
  • Convert input to appropriate type using Convert.To<type>()
  • Creating simple programs to add/subtract/multiply/divide numbers based on user input.

Control Structures

If-Statements

  • Basic usage to check conditions if (condition) { // Execute this if condition is true } else { // Execute this if condition is false }
  • Comparisons using logical operators: >, <, >=, <=, ==, !=, &&, ||
  • Complex if-else structures to handle multiple conditions

Switch Statements

  • Used to simplify complex if-else statements for checking multiple conditions switch(variable) { case value1: // Code block break; case value2: // Code block break; default: // Code block break; }

Loops

While Loops

  • Repeat a block of code while a condition is true while (condition) { // Code block }

Do-While Loops

  • Similar to while, but guarantees the block executes at least once do { // Code block } while (condition)

For Loops

  • Initialize, condition, and increment in a single line for (int i = 0; i < limit; i++) { // Code block }

Exception Handling

  • Try-Catch Blocks: Handle errors to prevent program crashes try { // Code block } catch (Exception e) { Console.WriteLine(e.Message); } finally { // Code block }
  • Multiple catches for specific exceptions (e.g., DivideByZeroException, FormatException)

Object-Oriented Programming

Classes and Objects

  • Class Definition: Blueprint for data types public class ClassName { public string attributeName; public ClassName(string parameterName) { this.attributeName = parameterName; } }
  • Object Creation: Instantiate and use objects ClassName obj = new ClassName("value"); Console.WriteLine(obj.attributeName);

Constructors

  • Special methods invoked during object creation for initializing attributes
public ClassName(string attr1, string attr2) { this.attr1 = attr1; this.attr2 = attr2; }

Object Methods

  • Define methods within classes to perform operations public class ClassName { public bool MethodName() { // Code block } }

Getters and Setters

  • Control access to attributes using properties private string varName; public string VarName { get { return varName; } set { varName = value; } }

Static Attributes and Methods

  • Static Attributes: Shared across all instances of a class public static int count = 0;
  • Static Methods: Called on the class itself rather than instances public static void StaticMethod() { // Code block }

Inheritance

  • Derive a class from another class, inheriting attributes and methods public class SubClass : SuperClass { // Additional or overridden functionality }
  • Method Overriding: Override a superclass method in a subclass using virtual and override keywords public virtual void SomeMethod() { // Code block } public override void SomeMethod() { // New code block }

Conclusion

  • Extensive potential applications in software development
  • Encourage further exploration and practice of C# concepts