0% found this document useful (0 votes)
98 views

C# Cheatsheet

The document provides an overview of key C# concepts including variables and data types, operators, control flow statements, classes and objects, inheritance, exception handling, generics, delegates, lambda expressions, and file input/output. It describes commonly used data types in C#, basic syntax for variables, operators, and control structures, and object-oriented programming concepts such as properties, methods, constructors, and inheritance. The document also covers collection types like arrays, lists, and dictionaries as well as advanced topics like generics, delegates, lambda expressions, and reading/writing files.

Uploaded by

Jinny jones
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

C# Cheatsheet

The document provides an overview of key C# concepts including variables and data types, operators, control flow statements, classes and objects, inheritance, exception handling, generics, delegates, lambda expressions, and file input/output. It describes commonly used data types in C#, basic syntax for variables, operators, and control structures, and object-oriented programming concepts such as properties, methods, constructors, and inheritance. The document also covers collection types like arrays, lists, and dictionaries as well as advanced topics like generics, delegates, lambda expressions, and reading/writing files.

Uploaded by

Jinny jones
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Console = Read, ReadLine, Write, WriteLine

str concat = +, console.writeline("{0}{1}", value, another value),


String.Format("",a,g,c)

bool, char, int long decimal(Huge), bigInteger(super Huge) float 7 double 14

datatype.MaxValue
variable.GetTypeCode()

var datatype

CASTING:
double pi = 3.233
int intPi = (int)pi;

Math:
.Ceiling, Floor, Max, Min, Pow, Round, Sqrt, Abs

Random Numbers:
Random rand = new Random();
rand.Next(1,11) // 11 is exclusive i.e 1-10

ternary operator ?

switch statement

goto within switch;

Cute:
Console.Write('Inside cute block')

do while loop

foreach in loop
randstr = 'paul'
foreach(char c in ransStr){

Strings:

static methods
String.IsNullOrEmpty(string)
.IsNullOrWhiteSpace(string)

Methods
string.Length
.IndexOf("")
.Substring(2,6)
.Equals(otherstring)
.StartsWith
.EndsWith
.Trim()
TrimStart
TrimEnd
Replace(search, replace)
Remove(0,2)

string[] names = new string[3]{"peter", "paul"}


String.Join(', ', names);

String.Format("{0:c}{1:00.00}{2:#.00}{3:0,0}", 1.56, 15.443, .56, 1000);

StringBuilder:
.Append()
.AppendLine()
.AppendFormat("format here", val, val)
.Clear()
.Replace()
.Remove()
.ToString()

Array:
int[] myarr;
int[] arr = new int[5];
int[] arr = {1,2,3,4,5}

.Length
Array.IndexOf(arra, val)
string.join(',', arr)
string.Split(',')

MultArray:

int[,] mult = new int[5,3];

int[,] mult = {{1,2}, {3,4}}

mult.GetLength(0)
mult.GetLength(1) = when using foreach loop to get length of

Lists

List<int> l = new List<int>()


.Add(5)

.AddRange(array) = to add array items to a list

.Clear

list = new List<int>(array)


list = new List<int>(new int[4])

Insert(1,2)
Remove(5)
RemoveAt()

IndexOf(val)
Contains("", StringComparer.OrdinalIgnoreCase)
.Sort

Exception Handling:

try:
int num = int.Parse(console.readlin())
catch:
ex.GetType().Name
ex.Message
throw new InvalidOperrationException('')
catch(Exception ex)

OOP

properties :

static int staticVal;

public double height {get; set;} Magic getters and setter

or

public string name;

public string Name{


get{return name;}
set { name = value; } //value is a reserved word
}

Can add multiple constructors based on type signature


Animal()
Animal(name,age)

public static int getStaticValue(){

public string toString(){}

method overloading, based on type signature / return type

named paramters to method (a: 34, b:36)

OBJECT INITIALIZER

Animal o = new Animal{


name = "",
h = ""
} instead of using constructor
>>>> CAN CREATE MULTIPLE CLASSES IN SAME FILE IN SAME NAMESPACE

INHERITANCE:

class Dog : Animal{


public Dog() : base(){
}
public Dog(int h, int b, int c) : base(h,b,c){

}
}

>>> To override a method from base class use 'new public int methodName'

>>>>> SEEMS override modifier is added to overridden abstract methods from abstract
class 'public override'

OPERATOR OVERLOADING

public static Rectangle operator+(Rectangle $rect1, Rectangle rect2){


//Do something with both rects and return a new rect
}

GENERICS

class KeyValue<Tkey, TValue>{

public TKey key{get; set;}


public TValue value {get; set;}

public KeyValue(Tkey k, Tvalue v{


key = k;
value = v;
}

KeyValue<string, string> superman = new KeyValue<string, string>("", "")

KeyValue<int, string> superman = new KeyValue<string, string>(0, "")

ENUMS:

pubic enum Temperature{

Freeze, // can set value e.g Freeze=1 to set counter


Low,
Warm,
Boil
}

Temperature temp = Temperature.Low


switch(temp)

case Temperature.Freeze:

break;
case Temperature.Low

...

STRUCTS

//Can have methods

struct Customers{

private string name;


private string balance
private string id;

public void createCust(...){


....
}
}

Customers b = new Customers;


b.createCust()

DELEGATES:

delegate double GetSum(double n1, double n2);

GetSum sum = delegate(double n1, double n2){


return n1 + n2;
}

LAMBDAS

Func<int, int, int> getSum = (x,y)=>x+y;

getSum.Invoke(3,4);

List<int> n = new List<int>{2,3,4,5,6};

List<int> odds = n.Where(a=> a % 2 == 1).ToList();

FILEIO

string[] custs = new string[]{"paul", "peter", "jeames"};


using (StreamWrite sw = new StreamWrite("file.txt")){
foreach(string cust in custs){
sw.WriteLine(cust);
}
}

string custName = '';

using(StreamReader sr = new StreamReader('file.txt')){


while((custName = sr.ReadLine()) != null){
Console.log(custName);
}
}

You might also like