C# Programming Cheatsheet: A Quick Reference Guide
C# Programming Cheatsheet: A Quick Reference Guide
This C# cheatsheet serves as a quick reference guide for beginners, covering fundamental concepts, syntax, and examples to enhance understanding of the language.
1. Introduction to C#
- C# (pronounced "C-sharp") is a modern, object-oriented programming language developed by Microsoft.
- It is widely used for developing Windows applications, web applications, and games.
2. Basic Syntax
A basic C# program consists of a namespace, class, and method:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
3. Data Types
C# supports various data types classified into:
- Value Types:
int
,float
,double
,char
,bool
- Reference Types:
string
, arrays, classes
Example:
int age = 25;
string name = "Alice";
4. Variables
Variables are declared using a type followed by a variable name:
int x; // Declaration
x = 10; // Initialization
5. Control Statements
Conditional Statements: if
, else
, switch
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
Loops: for
, while
, do-while
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
6. Functions
Functions (or methods) are blocks of code that perform specific tasks:
int Add(int a, int b)
{
return a + b;
}
7. Object-Oriented Concepts
Classes and Objects: C# is object-oriented, allowing for the creation of classes and objects.
class Car
{
public string Model;
public void Drive()
{
Console.WriteLine("Driving");
}
}
Inheritance: Allows a class to inherit from another class.
class Vehicle { }
class Car : Vehicle { }
Polymorphism: The ability of different classes to be treated as instances of the same class through a common interface.
8. Exception Handling
Use try
, catch
, and finally
to handle errors gracefully:
try
{
// Code that may cause an exception
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
9. Conclusion
This cheatsheet provides a foundational understanding of C# programming. Practice writing simple programs to solidify these concepts.
By following this cheatsheet, beginners can quickly grasp C# syntax and essential programming concepts!