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

MidTerm cheatsheet

Uploaded by

nahith35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

MidTerm cheatsheet

Uploaded by

nahith35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Linq method Example

Where (Filters) int[] evens = numbers.Where(num => num % 2 == 0).ToArray();


List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();

Console.WriteLine(string.Join(", ", evenNumbers)); // Output: 2, 4, 6

Contains List<string> fruits = new List<string> { "apple", "banana", "cherry" };


bool hasBanana = fruits.Contains("banana");

Console.WriteLine(hasBanana); // Output: True


FirstOrDefault int firstOrDefault = numbers.FirstOrDefault(); // Retrieves the first element or default if
empty
RemoveAll Numbers.RemoveAll(n => n == 2)
OrderBy List<int> orderedNumbersAsc = numbers.OrderBy(_ => _).ToList(); // Sorts numbers in
ascending order
OrderByDescending int[] orderedNumberDesc = numbers.OrderByDescending(_ => _).ToArray();
ThenBy (Second level sorting) List<Person> orderByNameAndAgeAsc = people
.OrderBy(person => person.Name)
.ThenBy(person => person.Age)
.ToList();
ThenByDescending List<Person> orderByNameAndAgeDesc = people
.OrderBy(person => person.Name)
.ThenByDescending(person => person.Age)
.ToList();
Reverse List<int> numbersReversed = numbers.AsEnumerable().Reverse().ToList(); // Reverses the
list order
Max int max = numbers.Max();
Min int min = numbers.Min();
Count int sum = numbers.Sum();
Sum int count = numbers.Count();
Any (check if any elements) List<int> numbers = new List<int> { 1, 3, 5, 7 };
bool hasEven = numbers.Any(n => n % 2 == 0);

Console.WriteLine(hasEven); // Output: False

List<Type> list = new List<Type>();

- list.Add(element); // Adds element to the end


- var element = list[index]; // Retrieves element at index
- list[index] = newElement; // Updates element at index
- list.RemoveAt(index); // Removes element at index
- int count = list.Count; // Returns the number of elements
- bool isEmpty = list.Count == 0; // Checks if the list is empty
- list.Clear(); // Removes all elements

- GEBRUIK “Contains” om te checken of een specifieke string/int/object is in een List of


dergelijke, het returned een bool: return Audience.Contains(audience);
- Private backing field gebruiken voor stack overflow :
private int _age;
public int Age
{
get => _age;
protected set => _age = value < 0 ? 0 : value;
}

Inventory: a read-only List of Items that starts out empty.


- ^Als ze dit zeggen dan betekent het:
public static readonly List<Item> Inventory = new List<Item>();
- Wanneer je wil dat een property is gelimiteerd van 0 tot 20:
set => base.Weight = Math.Clamp(value, 0, 20);

switch (variable)
{
case value1:
// code to execute if variable == value1
break;
default:
// code to execute if none of the cases match
break;
}

do
{
// code to execute at least once
} while (condition);

Random rand = new Random();


// Methods
int randomInt = rand.Next(); // Any int value
int randomIntInRange = rand.Next(min, max); // min (inclusive) to max (exclusive)
double randomDouble = rand.NextDouble(); // 0.0 (inclusive) to 1.0 (exclusive)
bool randomBoolean = rand.Next(0, 2) == 1; // Random true or false

Checking if string is not in format int:


string input = "two";
if (!int.TryParse(input, out _))
{
Console.WriteLine($"ID = {input}: invalid. The input string '{input}' was not in a correct
format");
}

You might also like