C Sharp Programming
This C# cheatsheet
This C# cheatsheet is designed for students and developers who want a quick reference to the syntax and features of C#. It covers basics, control structures, functions, OOP concepts, and more advanced topics.
Hello World Program
using System; class Program { static void Main() { Console.WriteLine("Hello, World!"); } }
Data Types
C# supports various data types, which define the kind of data a variable can hold.
int a = 10; // Integer float b = 3.14f; // Floating-point double c = 3.14159; // Double precision char d = 'A'; // Character bool e = true; // Boolean string s = "Hello"; // String
Variables and Constants
int age = 25; const int DAYS_IN_WEEK = 7;
Comments
Comments in C# help explain code and are ignored by the compiler.
Single-Line Comment
// This is a single line comment
Multi-Line Comment
/* This is a multi-line comment */
Input/Output
Console.WriteLine: Prints output to the screen.
Console.WriteLine("Hello World!");
Console.ReadLine: Reads input from the user.
string name = Console.ReadLine();
Control Structures
If-Else
int num = 5; if (num > 0) { Console.WriteLine("Positive"); } else { Console.WriteLine("Non-positive"); }
For Loop
for (int i = 1; i <= 5; i++) { Console.WriteLine(i); }
While Loop
int i = 1; while (i <= 5) { Console.WriteLine(i); i++; }
Switch Case
char grade = 'B'; switch (grade) { case 'A': Console.WriteLine("Excellent"); break; case 'B': Console.WriteLine("Good"); break; case 'C': Console.WriteLine("Fair"); break; default: Console.WriteLine("Invalid grade"); break; }
Functions
A block of code that performs a specific task.
int Add(int x, int y) { return x + y; }
Recursion
A function that calls itself to solve a problem.
int Factorial(int n) { if (n == 1) return 1; return n * Factorial(n - 1); }
Arrays
A collection of elements of the same type.
int[] numbers = {10, 20, 30, 40, 50}; for(int i = 0; i < numbers.Length; i++) { Console.Write(numbers[i] + " "); }
Strings
Strings are sequences of characters.
string str = "Hello, C#!"; Console.WriteLine(str);
Object-Oriented Programming
OOP in C# uses classes and objects. Key concepts include Class, Object, Constructor, Destructor, Encapsulation, Inheritance, Polymorphism.
Class
class Employee { public string Name; public int Age; }
Object
Employee emp = new Employee(); emp.Name = "Alice"; emp.Age = 25;
Constructor
class Employee { public Employee() { Console.WriteLine("Constructor called"); } }
Destructor
class Employee { ~Employee() { Console.WriteLine("Destructor called"); } }
Encapsulation
class Employee { private int salary; public void SetSalary(int s) { salary = s; } public int GetSalary() { return salary; } }
Inheritance
class Person { public string Name; } class Student : Person { public int RollNo; }
Polymorphism
class Base { public virtual void Show() { Console.WriteLine("Base"); } } class Derived : Base { public override void Show() { Console.WriteLine("Derived"); } }
File Handling
Reading from and writing to files.
using System.IO; File.WriteAllText("example.txt", "Hello, file!"); string content = File.ReadAllText("example.txt");