C# – Working with List of Collections

Use Generic Collections to work with items in a strongly typed fashion.

Better than arrays:

Know the type of the item for a certainty, no casting / converting

Better performance inserting / removing / updating

Collections provide more flexible actions to access items in the collection.

Allows for LINQ extension methods

Many different type of collections – specialities

“Generic Collections”

List

Dictionary <TKey, TValue>

T => Data type you need

“You make a generic specific by providing a data type”

List – only store strings (strong typed)

List – only store Cars in that collection

//Assume I have three objects: car1, car2, car3

List cars =  new List();

cars.Add(car1);

cars.Add(car2);

cars.Add(car3);

int numberOfCars = cars.Count;

Car myCar = cars.ElementAt(1); // returns second car in the element.

// Terminology: You access a member of a collection.

// LINQ Queries

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: