Welcome to your comprehensive C# basics cheat sheet! Whether you're a seasoned developer looking for a quick refresher or a newcomer eager to dive into the world of C#, this guide has got you covered. Let's embark on this coding journey together and explore the fundamentals of C# in a structured and engaging way.
C# Basics: Getting Started
Before we dive into the nitty-gritty of C#, let's ensure you have the right tools for the job. You'll need to install the .NET SDK, which includes the C# compiler and other essential tools. Once installed, you can create a new C# console application using the command line or your preferred IDE (Integrated Development Environment) like Visual Studio or Visual Studio Code.
Hello, World! Your First C# Program
Now that you're all set up, let's write your first C# program – the classic "Hello, World!" example. Create a new C# file (e.g., Program.cs) and add the following code:

```csharp using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello, World!"); } } ```
To run the program, simply press F5 or use the "Run" button in your IDE. You should see the output: "Hello, World!"
C# Syntax and Data Types
C# follows a syntax similar to other C-style languages like C++ and Java. It's case-sensitive, and statements end with a semicolon (;). Here are some basic data types in C#:
- Value Types: int, float, bool, char, struct
- Reference Types: class, interface, string
Here's how you declare and initialize variables in C#:

```csharp int myInt = 10; float myFloat = 3.14f; bool isValid = true; char myChar = 'A'; string myString = "Hello, World!"; ```
Control Structures: If, Else, Switch, and Loops
C# offers various control structures to manage the flow of your program. Here are some essential ones:
Conditional Statements
```csharp if (myInt > 5) { Console.WriteLine("myInt is greater than 5"); } else { Console.WriteLine("myInt is not greater than 5"); } switch (myInt) { case 1: Console.WriteLine("myInt is 1"); break; case 2: Console.WriteLine("myInt is 2"); break; default: Console.WriteLine("myInt is neither 1 nor 2"); break; } ```Loops
```csharp for (int i = 0; i < 5; i++) { Console.WriteLine(i); } while (myInt < 10) { Console.WriteLine(myInt); myInt++; } do { Console.WriteLine(myInt); myInt++; } while (myInt < 10); ```Functions and Methods
In C#, functions are called methods. Here's how you declare and use methods:
```csharp static void SayHello(string name) { Console.WriteLine($"Hello, {name}!"); } static int Add(int a, int b) { return a + b; } static void Main(string[] args) { SayHello("Alice"); int sum = Add(2, 3); Console.WriteLine(sum); } ```
Classes and Objects
C# is an object-oriented language, meaning it uses classes and objects to structure and organize code. Here's a simple example of a class and an object:

```csharp public class Person { public string Name { get; set; } public int Age { get; set; } public void Introduce() { Console.WriteLine($"Hi, I'm {Name} and I'm {Age} years old."); } } static void Main(string[] args) { Person alice = new Person { Name = "Alice", Age = 30 }; alice.Introduce(); } ```
This cheat sheet covers the basics of C#, providing you with a solid foundation to build upon. As you continue your learning journey, explore more advanced topics like exception handling, file I/O, and asynchronous programming. Happy coding!






















