×

C# Crash Course

Course Home C# Introduction and Setup C# Variables and Data Types C# Console Input and Output C# Operators and Expressions C# Conditional Statements - If and Else C# Switch Statements C# While and Do-While Loops C# For Loops C# Nested Loops C# Methods - Part 1 (Basics) C# Methods - Part 2 (ref, out, and Recursion) C# Arrays C# Number Systems (Binary and Hexadecimal) C# Exception Handling (Try-Catch) C# Random Numbers C# String Methods and Manipulation C# Course Summary and Best Practices


C# Introduction and Setup

What is C#?

C# (pronounced "C-Sharp") is a modern, object-oriented programming language developed by Microsoft. It runs on the .NET Framework and is used for: - Web applications - Desktop applications - Mobile apps - Games - And much more!

Why Learn C#?

  • Popular and in-demand: Used by many companies worldwide
  • Versatile: Can build various types of applications
  • Modern syntax: Easy to read and write
  • Strong typing: Helps catch errors early
  • Great tools: Visual Studio provides excellent development experience

Setting Up Your Environment

Required Software

  1. Visual Studio (Recommended for this course)
  2. Download Visual Studio Community (Free)
  3. Select ".NET desktop development" workload during installation

  4. Alternative: Visual Studio Code with C# extension

Creating Your First Console Application

Step 1: Open Visual Studio

  • Click on "Create a new project"
  • Select "Console App (.NET)" or "Console Application"
  • Choose a location (e.g., C:\Projects\MyFirstApp)
  • Name your project (e.g., HelloWorld)

Step 2: Project Structure

When you create a project, you'll see:

- Solution Explorer (shows your files)
- Program.cs (main code file)
- References (libraries your project uses)

Step 3: Understanding the Basic Structure

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Breaking it down: - using System; - Imports the System library (gives access to Console) - namespace HelloWorld - Organizes your code - class Program - Defines a class (container for code) - static void Main(string[] args) - Entry point of your program - Console.WriteLine() - Prints text to console

Your First Program

Let's write a simple program:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Welcome to C# Programming!");
            Console.WriteLine("\nPress any key to exit...");
            Console.ReadKey();
        }
    }
}

Running Your Program

  1. Press F5 or click the Start button
  2. Your program will run in a console window
  3. Press any key to close

Important Notes

  1. Add a comment block at the top with: csharp // Name: Your Name // Student Number: Your Student Number // Question Number: Question X.X ⚠️ You will lose 3 marks if you don't include this!

  2. Use proper naming conventions:

  3. Variables start with lowercase prefix + descriptive name
  4. Methods start with uppercase letter
  5. Use camelCase for variables: iMyNumber, sMyString

  6. Always include exit prompt: csharp Console.WriteLine("\nPress any key to exit..."); Console.ReadKey();

Folder Structure for Assignments

For practical tests/worksheets, create folders as specified:

T:\Practical_Test_1_YourStudentNumber\Question1
T:\Worksheet1_YourStudentNumber\Question1.1

Practice Exercise

Create a new console application that: 1. Prints your name 2. Prints your student number 3. Prints "I am learning C#!" 4. Waits for user input before closing

using System;

namespace MyIntroduction
{
    class Program
    {
        static void Main(string[] args)
        {
            // Your code here
            Console.WriteLine("Name: [Your Name]");
            Console.WriteLine("Student Number: [Your Number]");
            Console.WriteLine("I am learning C#!");

            Console.WriteLine("\nPress any key to exit...");
            Console.ReadKey();
        }
    }
}

Key Takeaways

✅ C# is a powerful, modern programming language ✅ Visual Studio is the recommended IDE ✅ Every program needs a Main method as entry point ✅ Always follow naming conventions and comment requirements ✅ Use Console.WriteLine() for output ✅ Use Console.ReadKey() to pause before exit


Next Topic: C# Variables and Data Types