; |
Used to separate statements from each other |
int i = 5; i++; Console.WriteLine(i); |
Script Execution |
// |
Used for single-line comments |
float multiplier = 0.01f; // % to float (e.g. 24% = 0.24) |
Comments |
Variable Initialization |
When a value is assigned to a variable for the first time |
int a = 5; |
Variables |
dotnet new console -o project-name |
For creating a new project using command prompt |
dotnet new console -o csharp-cheatsheet |
Console Basics Intro |
Script Execution Order |
Be mindful of chronology e.g. Maths |
N/A |
N/A |
Formatting |
? |
? |
? |
Console.WriteLine |
Write a comment in command. Use for testing & instructions. |
CW + TAB |
Printing Output |
Console.Write |
Doesn't create new line. Can chain multiple together. |
Console.Write("Ouput"); |
Comments |
Multi-Line Comment |
Turn more than one line of code into a comment |
/* Lines of text */ |
Comments |
XML Documentation Comment |
Use three forward slashes for special comments |
/// <summary> /// details |
Comments |
Variable |
Data storage units that consist of a name & a type. |
int x = 1; Console.WriteLine(x); |
Variables |
Variable Declaration |
Always declare a variable before using one |
int x; int y; int z; |
Variables |
Variable Assignment |
Use = sign to assign values to variables |
int z = (5+y) |
Variables |
Uninitialized Variable |
A variable that's declared, but lacks a definite assigned value |
int k; k = k + 1; |
Variables |
= (Assignment Operator) |
Assigns value to a variable, property, or indexer element. |
a = (b = c) |
Variables |
Scope |
Three levels: Class, Method, & Block level |
public class ClassLevel { everythingElse } |
N/A |
Variable Scope |
The reacha & validity of a variable |
{This.variableScope} |
Variables |
int |
Integer: whole numbers |
postive: 1, negative : -1 |
Basic Data Types |
float |
Fractional numbers + f (floating point numericals) |
0.25f |
Basic Data Types |
double |
Less accurate than float, but better performance & fractionals |
double a = 12.3; double bacteriaSize = 24e-10; |
Basic Data Types |
bool |
True or false literal flags |
bool check = true; bool isWinning = false; |
Basic Data Types |
char |
Uses unicode for single characters |
char euroSign = "€"; foreach(char c in helloWorld) {} |
Basic Data Types |
string |
A full string |
string name = "Harry"; use "@" before quotations for multiline |
Basic Data Types |
byte |
Assign variable within byte data range |
byte value1 = 64; byte someByte = 0xF1; |
Basic Data Types |
Implicit Casting |
Give a direct value |
int i = 5; double d = 1; 5.0 d = 9.78; |
Conversion |
Explicit Casting |
Converting fractional to whole comes with a loss of precision |
i = (int) d; // new value: 9 // string input = "2"; |
Conversion |
Type Conversion |
Convert between types e.g. class to variable |
int num = 2147483647; long bigNum = num; |
Conversion |
Convert.ToInt32 |
Convert to int e.g. from input |
i = Convert.ToInt32(input); |
Conversion |
Operators |
Functions represented as symbols |
int add = 3 + 5; |
Operators |
Arithmetic Operators |
Math operations |
public class Coin { public static Coins operator+ (Coin a, Coin b) {return new Coins (a, b);} |
Operators |
+ |
add |
int add = 3 + 5; // 8 |
Operators |
- |
sub |
int sub = 6 - 12; // -6 |
Operators |
* |
multiply |
int mul = 3 * 4; // 12 |
Operators |
/ |
divide |
int div = 9 / 3; // 3 |
Operators |
% |
modulus |
int mod = 10 % 3; // = 1 |
Operators |
+= |
assignment operator 1 |
a+=b -> a=a+b |
assignment operator 1 |
-= |
assignment operator 2 |
a−=b -> a=a−b |
assignment operator 1 |
++ |
incraments by 1 |
x++=11 |
Operators |
-- |
decraments by 1 |
`double a = 1.5; cw (--a); // output: 0.5 |
assignment operator 1` |
Post-Increment i++ |
increment the value then use it inside the expression |
int a, b = 15; a = b++; |
Operators |
Pre-Increment ++i |
increment the value of a variable before using it in an expression |
int a, b = 15; a = ++b; |
Operators |
System.Math |
A static class for solving math problems |
System.Math |
Math |
static |
Something that can't be initiated or inherited |
static void Main() {} |
Math |
Math.Max |
Finds the highest value |
int max = Math.Max(5, 3); // = 5 |
Math |
Math.Min |
Find the lowest value |
int min = Math.Min(5, 3); // = 3 |
Math |
Math.Sqrt |
Squareroot (what multiplied by itself equals this number) |
double sqrt = Math.Sqrt(16); // 4.0, because 4 * 4 = 16 |
Math |
Math.Abs |
Absolute (always positive, good for distance) |
double abs = Math.Abs(-4.3); // 4.3 |
Math |
Math.Round |
Uses statistical rounding to round to the nearest integer |
double round = Math.Round(12.6); // 13 |
Math |
Math.Floor |
Rounds to the lower integer |
double floor = Math.Floor(12.6) // 12 |
Math |
Math.Ceiling |
Rounds to the higher integer |
double ceil = Math.Ceiling(12.1); // 13 |
Math |
Math.Clamp |
Clamps an int between two defined values |
double clamp = Math.Clamp(15, 0, 10); // 15 is clamped to 10 |
Math |
Math.Pow |
Returns the value to the power of power |
double pow = Math.Pow(2, 3); // 8 (2^3 = 2 * 2 * 2 = 8) |
Math |
string.Length |
Returns the numerical length of a string |
string length = firstName.Length; |
Strings |
string.ToUpper |
Turns a string into ALL-CAPS. ToLower does the opposite. |
string upper = firstName.ToUpper(); // JOHN |
Strings |
string.+ |
String strings together |
string fullName = firstName + " " + lastName; // John Kane Strings |
|
$"{}" |
String strings and variables together using interpolation |
string interp = $"{firstName} {lastName}"; // John Kane |
Strings |
string.[] |
Get a certain letter or character from a string e.g. initials |
char char1 = firstName[0]; // 'J' |
Strings |
string.IndexOf |
Get an index of a first occurence of a character or string |
int index = firstName.IndexOf("h"); // 2 |
Strings |
string.SubString(int) |
Get a subpart of a string starting at an inputted value |
string sub = firstName.SubString(1); // "ohn" |
Strings string.Substring(int, int) |
string.Replace |
Replace all occurences of a character with another |
string replace = fullName.Replace( 'n', 'd'); // "Johd Kade" |
Strings |
immutable |
Something that can never be changed |
string fullName = "John Kane"; fullName = fullName.Replace('n', 'd'); cw(fullName); // Johd Kade |
Strings |
Logical Operators |
Logical constants used to connect two or more formulas |
Three types: and , or , and not |
Logical Operators |
! |
Inverse a logical operator |
!false -> true |
Logical Operators |
&& |
Combine two bool values [AND] |
bool x = true; bool y = true; bool z = x && y = true; // true |
Logical Operators |
Comparison Operators |
Compare two values to return a bool |
Equal to, greater than, less than etc. |
Comparison Operators |
> |
Greater than |
bool isGreater = 10 > 9; // True |
Comparison Operators |
== |
Is equal to |
bool isEqual = 10 == 9; // False |
Comparison Operators |
!= |
IS NOT equal to |
bool isNotEqual = 10 != 9; // True |
Comparison Operators |
|| |
Check if one of several values is true or false |
bool x = true; bool y = false; bool z = x && y = true; // true |
Logical Operators |
>= |
Greater OR equal to |
bool isGreaterOrEqual = 10>= 9; // True |
Comparison Operators |
<= |
? |
bool isLowerOrEqual = 10<= 9; // False |
? |
if |
Code that only executes if it's condition is met |
int hour = GetHour(); if (hour <12) { cw("Good morning!"); } |
If .. Else |
else if |
Alternate condition that could be met |
Else if (hour <18) { cw("Good Day!"); } |
If .. Else |
else |
If no other condition is met |
else { cw ("Good Evening!"); } |
If .. Else |
? : |
Ternary if-operator. A shortcut for: if something equals X do this, otherwise do that |
string greeting = hour < 12 ? "Good Morning!" : "Good Day!"; |
If .. Else |
Flow Control Statements |
Order in which statements or instructions are executed |
N/A |
If .. Else |
System.Random |
Creates Random values |
Random random = new Random(); |
Random |
pseudo-random |
? |
? |
? |
seed |
? |
? |
? |
Random.Next(int, int) |
? |
? |
? |
Random.Next() |
? |
? |
? |
Random.NextDouble() |
? |
? |
? |
Random.Next() |
? |
? |
? |
while |
? |
? |
? |
bool-expression |
? |
? |
? |
do..while |
? |
? |
? |
for |
? |
? |
? |
iteration statement |
? |
? |
? |
loop body |
? |
? |
? |
loop |
? |
? |
? |
execution |
? |
? |
? |
execution jump |
? |
? |
? |
break |
? |
? |
? |
continue |
? |
? |
? |
Array |
? |
? |
? |
int[] |
? |
? |
? |
Array Initialization |
? |
? |
? |
Array Access for Assignment |
? |
? |
? |
Array Access for Reading |
? |
? |
? |
Array.Resize |
? |
? |
? |
Array.Length |
? |
? |
? |
foreach |
? |
? |
? |
2D-Array |
? |
? |
? |
2D-Array Initialization |
? |
? |
? |
2D-Array Access for Assignment |
? |
? |
? |
2D-Array Access for Reading |
? |
? |
? |
Jagged Arrays |
? |
? |
? |
Method |
? |
? |
? |
void |
? |
? |
? |
Return Type |
? |
? |
? |
() |
? |
? |
? |
Parameter |
? |
? |
? |
Argument |
? |
? |
? |
Parameter |
? |
? |
? |
Parameter-List |
? |
? |
? |
Named Arguments |
? |
? |
? |
Optional Arguments |
? |
? |
? |
Default Value |
? |
? |
? |
return |
? |
? |
? |
Code Paths |
? |
? |
? |
Method Overloading |
? |
? |
? |
Object-Oriented Programming |
? |
? |
? |
Data |
? |
? |
? |
Function |
? |
? |
? |
Structured Programming |
? |
? |
? |
Objects |
? |
? |
? |
Instance Method |
? |
? |
? |
Class |
? |
? |
? |
Type |
? |
? |
? |
class |
? |
? |
? |
new |
? |
? |
? |
Class Member |
? |
? |
? |
Class Instance |
? |
? |
? |
Garbage Collector |
? |
? |
? |
null |
? |
? |
? |
Invoke |
? |
? |
? |
Field |
? |
? |
? |
Static Class Member |
? |
? |
? |
Static Class |
? |
? |
? |
Global Access |
? |
? |
? |
Constructor |
? |
? |
? |
Initial Class Values |
? |
? |
? |
Parameterless |
? |
? |
? |
Default Contructor |
? |
? |
? |
Finalizer |
? |
? |
? |
Object Destruction |
? |
? |
? |
GC.Collect |
? |
? |
? |
Encapsulation |
? |
? |
? |
Access Modifier |
? |
? |
? |
private |
? |
? |
? |
protected |
? |
? |
? |
public |
? |
? |
? |
internal |
? |
? |
? |
Class Member Access |
? |
? |
? |
Inheritance |
? |
? |
? |
Property |
? |
? |
? |
Getter Method |
? |
? |
? |
Setter Method |
? |
? |
? |
Validation |
? |
? |
? |
Processing |
? |
? |
? |
get |
? |
? |
? |
set |
? |
? |
? |
Expression Body Syntax |
? |
? |
? |
Auto Property |
? |
? |
? |
Read-Only Property |
? |
? |
? |
Auto Property |
? |
? |
? |
base-Class |
? |
? |
? |
Inherit From |
? |
? |
? |
Derived Class |
? |
? |
? |
Child Class |
? |
? |
? |
Parent Class |
? |
? |
? |
sealed |
? |
? |
? |
Polymorphism |
? |
? |
? |
as |
? |
? |
? |
virtual |
? |
? |
? |
override |
? |
? |
? |
base |
? |
? |
? |
Abstraction |
? |
? |
? |
abstract |
? |
? |
? |
Implementation |
? |
? |
? |
Composition |
? |
? |
? |
"Composition over Inheritance" |
? |
? |
? |