C# Cheat Sheet
C# Cheat Sheet
C# Introduction
Object-oriented language, with syntax similar to C++ and Java.
Type safe
Component oriented, structured language
Automatic garbage collection
Rich set of libraries
Conditional compilation
Syntax
Case sensitive
Comments are typed within // (single-line) or /**/ (multi-line)
Code is typed inside code blocks {}
Line termination is done using semicolon ;
Supports comment task highlighters like TODO: , NOTE: , WARN: etc…
Variables
<datatype> <variablename> = <initialvalue>;
Variables should start with underscore and cannot contain white spaces.
It can contain numbers but should always start with a capital letter.
It cannot contain any symbols (other than underscore).
Naming Conventions
Class StudentClass
Method GetMarks
Constant Percentile
Data types
https://hackr.io/blog/c-sharp-cheat-sheet 1/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Initialisation of variables
int i = 7;
byte b = 255;
String s = “hackr.io”;
char c = ‘h’;
Constant values
https://hackr.io/blog/c-sharp-cheat-sheet 2/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
dateVal =
AsDateTime() Convert string into datetime type str.AsDateTime();
Convert another data type like int, array, list etc into myVal = 1111;
ToString()
String strVal = myVal.ToString();
Operators
Operator Description
https://hackr.io/blog/c-sharp-cheat-sheet 3/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Logical AND
&&
if (isSingle && isMatching)
String Operations
https://hackr.io/blog/c-sharp-cheat-sheet 4/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
String
Definitions Example
Functions
https://hackr.io/blog/c-sharp-cheat-sheet 5/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Modifiers
field or function accessible by any other code in the same assembly or another
public
assembly that references it
protected Only accessible by code in the same class or struct or a derived class
internal Accessible by any code in the same assembly, but not from another assembly
https://hackr.io/blog/c-sharp-cheat-sheet 6/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
protected Accessible by any code in the same assembly, or by any derived class in another
internal assembly
to indicate a class that is intended only to be a base class of other classes (has to be
abstract
extended by other classes)
const Specifies that the value of the field or the local variable cannot be modified (constant)
override Provides a new implementation of a virtual member inherited from a base class
partial Defines partial classes, structs, and methods throughout the same assembly
Declares a field that can only be assigned values as part of the declaration or in a
read-only
constructor in the same class
Declares a member that belongs to the type itself instead of to a specific object, e.g., for
static
static class or method, no object needs to be created
Indicates that a field can be modified in the program by something such as the operating
volatile
system, the hardware, or a concurrently executing thread
Date/Time formatting
https://hackr.io/blog/c-sharp-cheat-sheet 7/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
DateTime.Now.ToShortDateString() prints only the date part by completely omitting the time part
https://hackr.io/blog/c-sharp-cheat-sheet 8/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Full date/time
Represents a combination of the long date (D) and short time (t)
f pattern (short
patterns, separated by a space.
time)
General
date/time Represents a combination of the short date (d) and short time (t)
g
pattern (short patterns, separated by a space.
time)
General
date/time Represents a combination of the short date (d) and long time (T)
G
pattern (long patterns, separated by a space.
time)
https://hackr.io/blog/c-sharp-cheat-sheet 9/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Formatting does not modify the value of the DateTime object that is
being formatted. Therefore, the application must convert the value to
Coordinated Universal Time (UTC) before using this format specifier.
https://hackr.io/blog/c-sharp-cheat-sheet 10/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Custom
patterns –
"MM'/'dd
yyyy" 03/17 2019
"dd.MM.yyyy" 17.03.2019
Custom
format "MM.dd.yyyy 03.17.2019 06:23
HH:mm"
Tuesday, march (2019) : 06:23:00
"dddd,
MMMM
(yyyy):
HH:mm:ss"
Any
other (Unknown
An unknown specifier throws a runtime format exception.
single specifier)
character
Arrays
For creating, modifying, sorting and searching arrays.
https://hackr.io/blog/c-sharp-cheat-sheet 11/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
arrVal.IsFixedSize;
Array.Clear(arrVal, 0, 2);
Sets a range of elements in an array to If arrVal is an array of integers, the
Clear()
the default value of each element type. elements at position 0 to 2 will be
set to zero after doing Clear().
https://hackr.io/blog/c-sharp-cheat-sheet 12/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Array.ConstrainedCopy(srcArr, 0,
destArr, 3, 5);
Copies a range of elements from an Array where srcArr is the source array,
starting at the specified source index and 0 is the start index from where copy
pastes them to another Array starting at should begin,
ConstrainedCopy()
the specified destination index.
Guarantees that all changes are undone if destArr is the destination array,
the copy does not succeed completely. 3 is the place where copy should
start in the destination array,
conArr = Array.ConvertAll(arrVal,
Converts an array of one data type to an
ConvertAll() new Converter<dtype1, dtype2>
array of another data type.
(method));
https://hackr.io/blog/c-sharp-cheat-sheet 13/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
https://hackr.io/blog/c-sharp-cheat-sheet 14/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
https://hackr.io/blog/c-sharp-cheat-sheet 15/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Control Statements
if (true) {...}
if-else else if (true) {...}
else {...}
switch (var)
{
case 1: break;
switch
case 2: break;
default: break;
do {...}
do... while
while (true);
https://hackr.io/blog/c-sharp-cheat-sheet 16/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
try {...}
catch (Exception e) {...}
try-catch-finally
catch {...}
finally {...}
Regular Expressions
+ match one or more occurrence
? match 0 or 1 occurrence
a|b either a or b
\n new line
\r carriage return
\t tab
Collections
Arraylist
Capacity Gets or sets the number of elements that the ArrayList can contain.
https://hackr.io/blog/c-sharp-cheat-sheet 17/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
IsFixedSize Gets a value indicating whether the ArrayList has a fixed size.
AddRange(ICollection c); Adds the elements of an ICollection to the end of the ArrayList.
GetRange(int index, int Returns an ArrayList which represents a subset of the elements in the
count); source ArrayList.
InsertRange(int index, Inserts the elements of a collection into the ArrayList at the specified
ICollection c); index.
Remove(object obj); Removes the first occurrence of a specific object from the ArrayList.
RemoveAt(int index); Removes the element at the specified index of the ArrayList.
RemoveRange(int index,
Removes a range of elements from the ArrayList
int count);
SetRange(int index, Copies the elements of a collection over a range of elements in the
ICollection c); ArrayList.
https://hackr.io/blog/c-sharp-cheat-sheet 18/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
TrimToSize(); Sets the capacity to the actual number of elements in the ArrayList.
Hashtable
IsFixedSize Gets a value indicating whether the Hashtable has a fixed size
Item Gets or sets the value associated with the specified key.
Add(object key, object value); Adds an element with the specified key and value into the Hashtable
Remove(object key); Removes the element with the specified key from the Hashtable.
SortedList
Item Gets and sets the value associated with a specific key in the SortedList.
https://hackr.io/blog/c-sharp-cheat-sheet 19/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
ContainsKey(object
Checks if the SortedList contains a specific key.
key);
ContainsValue(object
Checks if the SortedList contains a specific value.
value);
GetByIndex(int index); Gets the value at the specified index of the SortedList.
GetKey(int index); Gets the key at the specified index of the SortedList.
IndexOfKey(object
Returns the zero-based index of the specified key in the SortedList.
key);
IndexOfValue(object Returns the zero-based index of the first occurrence of the specified value
value); in the SortedList.
Remove(object key); Removes the element with the specified key from the SortedList.
TrimToSize(); Sets the capacity to the actual number of elements in the SortedList.
Stack
https://hackr.io/blog/c-sharp-cheat-sheet 20/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Peek(); Returns the object at the top of the Stack without removing it.
Pop(); Removes and returns the object at the top of the Stack.
Queue
Dequeue(); Removes and returns the object at the beginning of the Queue.
TrimToSize(); Sets the capacity to the actual number of elements in the Queue.
Dictionary
Item Gets or sets the element with the specified key in the Dictionary<TKey,TValue>.
https://hackr.io/blog/c-sharp-cheat-sheet 21/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Remove Removes the first occurrence of specified item from the Dictionary<TKey, TValue>.
Returns true and assigns the value with specified key, if key does not exists then
TryGetValue
return false.
Exception Handling
try{
} catch (Exception e){
throw;
}
Methods
static method, no object needed to call method public static void MyMethod(){}
Classes
https://hackr.io/blog/c-sharp-cheat-sheet 22/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
Class MyClass
{
/*Class definition*/
}
Object creation –
MyClass ClassObj = new MyClass();
Partial Class
Classes within the same namespace can be split into smaller classes with same name.
// PartialClass1.cs // PartialClass2.cs
using System; using System;
namespace PartialClasses namespace PartialClasses
{ {
public partial class PartialClass public partial class PartialClass
{ {
public void HelloWorld() public void HelloUser()
{ {
Console.WriteLine("Hello, world!"); Console.WriteLine("Hello, user!");
} }
} }
} }
File Handling
Check the existence of the file in
File.Exists File.Exists(path)
the specified path
File.ReadAllLines(path)
Read all the lines from the file
File.ReadAllLines Console.WriteLines(File.ReadAllLines(path)
specified by the path
// Write to console
https://hackr.io/blog/c-sharp-cheat-sheet 23/24
8/24/22, 12:13 PM Download C# Cheat Sheet PDF for Your Quick Reference
https://hackr.io/blog/c-sharp-cheat-sheet 24/24