paint-brush
Learn C#: A Cheat Sheet for Newcomersby@ssukhpinder
1,728 reads
1,728 reads

Learn C#: A Cheat Sheet for Newcomers

by Sukhpinder SinghMarch 9th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

C# is a modern, object-oriented programming language developed by Microsoft. It is widely used for creating desktop applications, games, mobile applications, and web services. As with any programming language, there are a lot of concepts, syntax, and libraries to learn. This cheat sheet covers the most critical aspects of the language.
featured image - Learn C#: A Cheat Sheet for Newcomers
Sukhpinder Singh HackerNoon profile picture

Programming Made Easy With This Quick Reference Cheat Sheet


C# (pronounced “C sharp”) is a modern, object-oriented programming language developed by Microsoft. It is widely used for creating desktop applications, games, mobile applications, and web services.


As with any programming language, there are a lot of concepts, syntax, and libraries to learn. In this article, we will provide a cheat sheet that covers the most important aspects of C# programming.

C# Syntax Cheat Sheet

Variables and Data Types

C# supports data types like int, float, double, char, bool, etc. Declare variables with their respective data types as follows:

int x = 5;
float f = 3.14f;
double d = 3.14159;
char c = 'a';
bool b = true;

Control Flow Statements

Control flow statements like if-else, switch-case, for loop, while loop, and do-while loop are used to control the flow of program execution.

if (condition)
{
   // code to execute if condition is true
}
else
{
   // code to execute if condition is false
}

switch (variable)
{
   case value1:
      // code to execute if variable == value1
      break;
   case value2:
      // code to execute if variable == value2
      break;
   default:
      // code to execute if variable doesn't match any of the cases
      break;
}

for (int i = 0; i < 10; i++)
{
   // code to execute in a loop
}

while (condition)
{
   // code to execute while condition is true
}

do
{
   // code to execute at least once, then while condition is true
}
while (condition);

Arrays and Collections

Arrays and collections store multiple values of the same data type.

int[] numbers = { 1, 2, 3, 4, 5 };

List<int> numbersList = new List<int>();
numbersList.Add(1);
numbersList.Add(2);
numbersList.Add(3);

Classes and Objects

Classes and objects are the building blocks of object-oriented programming in C#. A class is a blueprint that defines the properties and behaviors of an object.

class MyClass
{
   int x;
   void MyMethod()
   {
      // code to execute
   }
}

MyClass myObject = new MyClass();

Inheritance and Polymorphism

Inheritance is a mechanism by which a class derives properties and behaviors from a parent class. Polymorphism allows a child class to take on the properties and behaviors of its parent class.

class ParentClass
{
   void MyMethod()
   {
      // code to execute
   }
}

class ChildClass : ParentClass
{
   // additional properties and behaviors of the child class
}

ParentClass myObject = new ChildClass();

Exception Handling

Exception handling is a way to handle runtime errors and prevent application crashes.

try
{
   // code that may throw an exception
}
catch (Exception ex)
{
   // code to handle the exception
}
finally
{
   // code to execute regardless of whether an exception was thrown
}

File Input and Output

C# provides built-in classes for reading from and writing to files.

using System.IO;

// write to a file
StreamWriter writer = new StreamWriter("myfile.txt");
writer.WriteLine("Hello, world!");
writer.Close();

// read from a file
StreamReader reader = new StreamReader("myfile.txt");
string line = reader.ReadLine();
reader.Close();


In conclusion, the C# programming language has a lot of concepts to learn, but this cheat sheet covers the most critical aspects of the language. By mastering these concepts, you’ll be well on your way to becoming a proficient C# programmer.


It’s important to note that this cheat sheet is meant to be a partial guide to C# programming. Many other topics, such as LINQ, delegates, events, and asynchronous programming, should be covered in this cheat sheet.


However, this cheat sheet should give you a solid foundation to build upon as you continue to learn and explore the language.


When using this cheat sheet, it’s essential to remember that good programming practices and clean code are just as important as knowing the syntax and language features. Always strive to write code that is easy to read, maintain, and debug.


To summarize, here’s a recap of the C# syntax cheat sheet:

  • Variables and Data Types
  • Control Flow Statements
  • Arrays and Collections
  • Classes and Objects
  • Inheritance and Polymorphism
  • Exception Handling
  • File Input and Output

Follow me on

C# Publication, LinkedIn, Instagram, Twitter, Dev.to


Also published here