Show Menu

C# / .NET Introduction Cheat Sheet (DRAFT) by

An introduction and basics cheat sheet for the C# programming language.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Whats What

A class can contain zero or more methods, zero or more constr­uctors, and zero or more variables
A method declares, simply, an action and, if necessary, response in the form of a 'return'. Methods can take variables as arguments to pass data along
A method that auto-e­xecutes upon the creation of a new instance of a class
The "­out­put­" variable of a method used to return data to the method's caller
Argument / Parameter
An "­inp­ut" variable for a method used when calling said method to pass data to it

Good Beginner Projects

Chess Game - Create a game of chess with a UI (WPF / WinForms) and utilizing icons from FontAw­esome that can be played between two players

Calculator - Create a calculator with a UI (WPF / WinForms) that can be used to perform basic mathem­atical operations on two numbers (addition, subtra­ction, multip­lic­ation, division, square root, exponents)

To-Do List - Create a simple UI (WPF / WinForms) where to-do items can be created, and marked off. Marked off to-do's should be retrie­vable after marked as complete

Weight Conversion Tool - A simple console or UI applic­ation where a weight type (lbs, kgs, oz) can be converted to any other weight type

Single­-page Website - Utilize ASP.NET and the options presented when creating a new project to build a single­-page website for yourself

Diagnosing Exceptions (A.K.A. Runtime Errors)

Error Message
Most Likely Cause
A variable or method is refere­ncing a variable or method that doesn't exist or has been disposed of
Most often caused by too many consec­utive method calls
Caused by an attempt to access (read/­write) to an array object that doesn't yet exist, or is outside the min/max bounds of the array or list
A failure to cast, convert, or apply arithmetic (mathe­mat­ical) operations on one or more variables
When a floati­ng-­point (float) variable reaches positive or negative infinity
A failure most often caused by an inability to read or write to a file or folder (either because of permis­sions, or a locked file)
Program is unable to find the file in question, and cannot perform any operation going forward
Same as above, except in reference to a folder or directory

Object Types

int - Integer
double - Floating binary­-point number (exact)
float - Floating binary­-point number (estimate)
decimal - Decimal number (exact)
string - Char array
char - Character
bool - True/false boolean
var - Dynamic variable

Using Statements

Using statements are declar­ations of which code libraries are being utilized within this file / class. Additi­onally, if declared similarly to a variable, a shortened keyword can be used in place of a full-l­ength declar­ation further within the code.

For example:
using System;

using System.Di­agn­ostics;

using System.Wi­ndo­ws.F­orms;

using Excel = Micros­oft.Of­fic­e.I­nte­rop.Excel;

Using the Console

var input = Console.ReadLine();
Gets the last line input to the console and saves it to variable 'input'
Writes the text-r­epr­ese­ntation of the variable (any) and does not move to the next line
Writes the text-r­epr­ese­ntation of the variable (any) and moves to the next line
Use a variable name in place of {{x}}


Single­-line comment
Begin multi-line comment
End multi-line comment

Declaring a Variable

Declaring a variable is as easy as knowing the type of variable needed, a name for the variable, and the default (a.k.a. initial) value for said variable.

{Variable Type} {Variable Name} = {Default / Initial Value};

Say we need to create an integer named "­ite­mCo­unt­":
int itemCount = 0;

Now what about a true/false statement named "­val­ida­ted­":
bool validated = false;

A variable based off a class named "­Car­" with a variable named "­new­Car­":
Car newCar = new Car();

Declaring a Method

Declaring a method is nearly as simple as declaring a variable, you must know the publicity of the method, the return type (if one is needed), the name of the method, and any arguments (data) that is needed by the method. Remember, you create the name of the method.

{Publi­city} {Return Type} {Method Name} ({Argu­ments})

Lets say we need a public method that returns an integer named "­Add­" that takes two integers as arguments:
public int Add (int number1, int number2)

We can also declare a method with an optional parameter by declaring an argument with a default value:
public int Add (int number1, int number2, int number3 = 0)

Altern­ati­vely, we can create methods that don't return anything:
public void DoSome­thing ()

Publicity / Access Modifiers

Accessible from any class within any assembly or namespace
Accessible from any class within only this assembly
Accessible only by this class and any class derived or inherited by this class
Accessible only by this class
Access modifiers can be used on Methods, Classes, and even Variables, to allow for granular access to only the necessary aspects of your code.

Access Modifiers

Utilizing Git (Building a Portfolio) WIP

Firstly, ensure you have a GitHub or other Git applic­ation account. Most beginning / solo developers prefer GitHub for its ease-o­f-use and issue tracking capabi­lities.

Once an account is created, create a project (a.k.a. reposi­tory) and name it approp­riately in the applic­ation, include as much detail, and a descri­ption, as possible.

From the newly created project's dashboard, look for a button to clone the repository to your machine. A few options may be presented, and if using both Visual Studio and GitHub, the 'Clone to Visual Studio' magnet button is a quick and efficient way of cloning the repository locally.

Logical Operators

Equal to
Not equal to
And (i.e. A == 1 && B == 2)
Or (i.e. A == 1 || B == 2)
Greater than or equal to
Less than or equal to
Greater than
Less than