Beginner's Guide to C# Programming

Sep 4, 2024

Introduction to C# for Beginners

Overview

  • C# is a flexible language used for:
    • Console apps
    • Web services
    • Game development
  • Average salary for a C# developer: $63,000/year

Getting Started with C#

  • IDE Requirement: Use Visual Studio Community
  • Installation:
    • Download from the official website
    • Choose .NET desktop development package
    • Optionally choose Unity package

Basic C# Program

  • Create a new C# console application
  • Main Method: Entry point of a C# program
    • static void Main(string[] args)
    • Code executes from top to bottom within the method

Output in C#

  • Use Console.WriteLine() to display text
  • Standard Output:
    • Example: Console.WriteLine("Hello World")
  • Modify Output: Change text within quotes

Customizing Console Window

  • Font and Size:
    • Right-click top left corner > Properties > Font
  • Color and Layout:
    • Change text color and window size

Beep Sound

  • Make the console beep with Console.Beep()

Introduction to Variables

  • Variables store data values
  • Declaration and Initialization:
    • Declare: int x;
    • Initialize: x = 123;
  • Data Types:
    • int: Whole numbers
    • double: Decimal numbers
    • bool: True/False values
    • char: Single character
    • string: Text

Typecasting

  • Convert data types explicitly
  • Examples:
    • Convert double to int: Convert.ToInt32(value)

User Input

  • Read input with Console.ReadLine()
  • Convert input to required data type

Basic Arithmetic

  • Operators: +, -, *, /, %
  • Increment/Decrement:
    • Increment: i++ or i += 1
    • Decrement: i-- or i -= 1

Math Class

  • Useful methods:
    • Math.Pow(base, power)
    • Math.Sqrt(value)

Random Numbers

  • Use Random class to generate random numbers
  • Methods:
    • Random.Next(min, max)
    • Random.NextDouble()

Control Structures

  • If Statements: Decision-making
  • Switch Statements: Efficient alternative to multiple if-else
  • Logical Operators: && (and), || (or)

Loops

  • While Loop: Repeats code while condition is true
  • For Loop: Repeat a specific number of times
  • Nested Loops: Loop within a loop

Arrays

  • Store multiple values of the same type
  • Access elements using index
  • Multi-dimensional Arrays: Arrays of arrays

Methods

  • Reuse code without rewriting
  • Parameters: Pass data to methods
  • Return Keyword: Return data from method

Objects and Classes

  • Classes: Blueprint for creating objects
  • Objects: Instance of a class
  • Constructors: Special method to initialize objects

Inheritance

  • Child classes receive fields and methods from parent class

Abstract Classes

  • Cannot instantiate objects
  • Defines base class for other classes

Interfaces

  • Define a contract for classes

Lists

  • Dynamic arrays that can change size
  • Useful methods: Add(), Remove(), Contains()

Generics

  • Allows for code reusability for different data types

Exceptions

  • Handle errors using try-catch blocks

Multithreading

  • Perform tasks concurrently using threads

Conclusion

  • Practice and explore more advanced concepts like delegates, events, and LINQ for further mastery of C# programming.