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
- Visual Studio (Recommended for this course)
- Download Visual Studio Community (Free)
-
Select ".NET desktop development" workload during installation
-
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
- Press F5 or click the Start button
- Your program will run in a console window
- Press any key to close
Important Notes
-
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! -
Use proper naming conventions:
- Variables start with lowercase prefix + descriptive name
- Methods start with uppercase letter
-
Use camelCase for variables:
iMyNumber
,sMyString
-
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