C#

Data Types:

Data types in C# are referred to as "value types" and "reference types".

When a value type is declared, the data is stored in the memory location of the variable. When a reference type is declared, the data is stored somewhere else in memory and the variable contains a pointer it.

  • Reference types are always allocated in heap memory,

  • value types can be allocated on the heap or on the stack depending on where they're declared.

If a value type is declared inside a method, then it's stored on the stack. If it's declared in a class, then the data is stored on the heap. This is because a class is a reference type, which must always be stored on the heap.

Value types include int, byte, bool, and char. Reference types include string, arrays and class.

Integers:

An integer to be negative, it should be declared as a "signed" integer. Otherwise, it can be "unsigned". Unsigned integers can be larger than signed integers because we're not wasting a "sign" bit. We can allocate different sizes for an integer in multiples of 8 bits: 8, 16, 32 and 64 bits.

Keyword
Size

sbyte

Signed 8-bit

byte

Unsigned 8-bit

short

Signed 16-bit

ushort

Unsigned 16-bit

int

Signed 32-bit

uint

Unsigned 32-bit

long

Signed 64-bit

ulong

Unsigned 64-bit

A floating point is a number that can have decimal places, 8.2, 3.14, etc. declaring a float or double, the letter F or D should be appended to the value. There are three types (all of which are signed):

Keyword
Size

float

4 bytes

double

8 bytes

decimal

16 bytes

A bool is a true or false value

A char is a single letter or number, represented by an integer

The array and tuple are both types of collections

An array can hold multiple values of a single data type; whereas a tuple can hold multiple values but of various data types

A tuple is declared using parenthesise for both the data types and initial values; and instead of accessing a tuple index via square brackets, we use the period, .. Each item is given a name like Item1, Item2, etc. Can use a concept called "deconstruction" to assign friendly variable names to your elements.

One way is via interpolation, denoted by a prepended $. Another is by using the + operator. Or by using the string.Concat()

string has lots of really useful methods including Split, Join, Equals, and IsNullOrEmpty/IsNullOrWhiteSpace

C# uses different text casing depending on where it's being declared. Here is a summary of the conventions:

Object
Casing

Classes

PascalCase

Public Members

PascalCase

Private Members

_camelCase

Methods

PascalCase

Variables

camelCase

Enums

PascalCase

lists:

Dictionaries:

hashtables:

Queues:

FIFO:

LIFO:

Bitwise Operators:

If / Else:

For Loops:

  • Statement 1 is executed one time at the start of the loop.

  • Statement 2 defines the condition for executing the loop code.

  • Statement 3 is executed after every loop.

While Loop:

Taking Arguments:

Input:

Object orientation:

Last updated