📘 Table of Contents
1. Introduction to C#
C# (pronounced “C-sharp”) is a modern, object-oriented language developed by Microsoft. It’s widely used for desktop applications, web apps, and game development with Unity.
2. Setting Up Environment
To write and run C# programs, install:
- Visual Studio or Visual Studio Code
- .NET SDK from dotnet.microsoft.com
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, C#!");
}
}
3. Basic Syntax
Each C# program starts with a Main() method.
4. Variables and Data Types
int age = 25;
string name = "Alice";
double price = 9.99;
bool isOnline = true;
5. Conditionals and Loops
for (int i = 1; i <= 5; i++) {
Console.WriteLine(i);
}
if (age > 18) {
Console.WriteLine("Adult");
}
6. Methods
static int Add(int a, int b) {
return a + b;
}
7. Object-Oriented Programming
class Person {
public string Name;
public void Greet() {
Console.WriteLine($"Hello, {Name}!");
}
}
class Program {
static void Main() {
Person p = new Person();
p.Name = "John";
p.Greet();
}
}
8. Best Practices
- Follow PascalCase for class names and methods.
- Use meaningful variable names.
- Keep methods short and focused.
9. Mini Project — Simple Calculator
using System;
class Calculator {
static void Main() {
Console.Write("Enter first number: ");
double a = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter operator (+, -, *, /): ");
char op = Console.ReadLine()[0];
Console.Write("Enter second number: ");
double b = Convert.ToDouble(Console.ReadLine());
double result = 0;
switch(op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/': result = a / b; break;
default: Console.WriteLine("Invalid operator."); return;
}
Console.WriteLine($"Result: {result}");
}
}
10. Conclusion
C# is powerful, elegant, and versatile — ideal for building secure and scalable applications across platforms.
Comments
Post a Comment