C# is a modern, object-oriented programming language developed by Microsoft. It’s part of the .NET ecosystem and is used for building desktop apps, games (Unity), APIs, enterprise software, and mobile apps.
Why Learn C#?
- Easy syntax (similar to Java & C++).
- Cross-platform with .NET Core/.NET 6+.
- Widely used in game development (Unity).
- Powerful for web apps via ASP.NET.
Hello World
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, C#!");
}
}
Variables & Data Types
int age = 25;
double pi = 3.14;
char grade = 'A';
string name = "Alice";
bool isStudent = true;
Input & Output
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);
Operators
- Arithmetic:
+, -, *, /, %
- Comparison:
==, !=, <, >, <=, >=
- Logical:
&& (AND), || (OR), ! (NOT)
Conditionals
int age = 20;
if (age >= 18) {
Console.WriteLine("Adult");
} else {
Console.WriteLine("Minor");
}
Loops
// For loop
for (int i = 1; i <= 5; i++) {
Console.WriteLine(i);
}
// While loop
int x = 0;
while (x < 3) {
Console.WriteLine(x);
x++;
}
Functions
static int Add(int a, int b) {
return a + b;
}
static void Main() {
Console.WriteLine(Add(3, 4));
}
Arrays & Lists
// Array
int[] numbers = {1, 2, 3};
// List
using System.Collections.Generic;
List<string> fruits = new List<string>() {"Apple", "Banana"};
fruits.Add("Cherry");
Strings
string name = "Alice";
Console.WriteLine("Length: " + name.Length);
Console.WriteLine("First letter: " + name[0]);
Object-Oriented Programming (OOP)
class Person {
public string Name;
public int Age;
public void Greet() {
Console.WriteLine("Hi, I'm " + Name);
}
}
class Program {
static void Main() {
Person p1 = new Person();
p1.Name = "Alice";
p1.Age = 25;
p1.Greet();
}
}
Properties & Encapsulation
class Person {
private string name;
public string Name {
get { return name; }
set { name = value; }
}
}
class Program {
static void Main() {
Person p = new Person();
p.Name = "Alice";
Console.WriteLine(p.Name);
}
}
File Handling
using System.IO;
class Program {
static void Main() {
File.WriteAllText("data.txt", "Hello, File!");
string content = File.ReadAllText("data.txt");
Console.WriteLine(content);
}
}
Mini Project: Simple Calculator
using System;
class Calculator {
static int Add(int a, int b) => a + b;
static int Sub(int a, int b) => a - b;
static int Mul(int a, int b) => a * b;
static double Div(int a, int b) => (double)a / b;
static void Main() {
Console.WriteLine(Add(5, 3));
Console.WriteLine(Sub(10, 4));
Console.WriteLine(Mul(2, 6));
Console.WriteLine(Div(8, 2));
}
}
Popular Frameworks & Uses
- ASP.NET → Web development
- Unity → Game development
- Xamarin / .NET MAUI → Mobile apps
- WPF / WinForms → Desktop apps
Best Practices
- Follow .NET naming conventions (PascalCase for methods/classes).
- Use
var
for type inference when clear. - Prefer properties over public fields.
- Use exception handling with
try/catch
.
Next Steps
- Learn OOP deeply (inheritance, polymorphism, interfaces).
- Explore async programming with
async/await
. - Build small apps (to-do list, calculator, CRUD app).
- Explore Unity for games or ASP.NET for web APIs.
Conclusion
C# is a modern, versatile, and powerful language for beginners and pros alike. Whether you want to build games, enterprise apps, or web APIs, C# and the .NET ecosystem have you covered. 🚀
No comments:
Post a Comment