Arrays and Lists Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Arrays

A

Description: Arrays in C# are fixed-size collections of elements of the same type. They offer fast access to elements by index but cannot change in size once defined.

Usage:

Use arrays when you know the number of elements you need beforehand and it won’t change.
Often used for performance-critical operations where direct memory access is beneficial.

Basic Operations:

Initialization: int[] scores = new int[5]; creates an integer array of size 5.

Accessing Elements: int value = scores[2]; accesses the third element (index 2) of the array.

Iterating: Use for loops or foreach loops to iterate through elements.
Length: Access the number of elements with scores.Length.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Lists

A

Description: Lists are dynamic arrays in C# that can grow or shrink in size at runtime. They are part of the System.Collections.Generic namespace.

Usage:

Use lists when you need a collection that can change in size dynamically.
Suitable for scenarios where you frequently add or remove elements.

Basic Operations:

Initialization: List<int> numbers = new List<int>(); creates an empty list of integers.
Adding Elements: numbers.Add(10); adds an element to the end of the list.
Removing Elements: numbers.Remove(10); removes the first occurrence of the specified element.
Accessing Elements: int value = numbers[0]; accesses the first element of the list.
Iterating: Use foreach loops or for loops with Count property for iteration.
Count: Access the number of elements with numbers.Count.</int></int>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly