MIS 324 -- Professor Sandvig MIS 324 Professor Sandvig 11/9/2018 C# Collections MIS 324 Professor Sandvig
Overview What are collections Arrays Language Integrated Query (LINQ) Sort & filter collections Similar to SQL Generic List<>
What are Collections Collections store multiple data values Frequently work with collections: Database records List of products List of people Shopping cart Cookies Web form values Etc.
What Are Collections Arrays .NET library namespaces C# feature Simple Limited functionality .NET library namespaces System.Collections System.Collections.Generic Classes for managing collections of data
What are Collections Select collection type based on desired behavior & performance Many choices: Array Queue Stack Dictionary List<> Sorted List Etc.
What are Collections A few types of collections: Collection Best for Dictionary Fast lookups Sorted list Items sorted on Insert List General use Stack Last in First Out (LIFO) Queue First in First Out (FIFO)
Array Simple collection type Basic C# language feature Limited features No sorting No filtering Limited to C# data types
Array Advantages Disadvantages Simple Basic features: Iterate, retrieve values, find value Disadvantages Limited to C# data types String, Int, Double, etc. Size fixed when initialized
LINQ Language Integrated Query Query language built into C# Use with any collection type Including database Capabilities similar to SQL Uses Lamda Syntax .where(collection => expression) Collection Examples
Collections Namespaces .NET library namespaces System.Collections System.Collections.Generic Have similar classes List Stack Queue Etc. Generic classes newer More functionality
Generic Collections System.Collections.Generic namespace Strongly typed! Faster Used extensively in MVC Retain datatypes defined in model .NET Library Documentation
Generic Collections Why “generic”? Declare type when create collection Items in collection can be any “generic” object Declare type when create collection List<int> myGenericList = new List<int>(); Generics can hold any object List<PersonModel> personList = new List<PersonModel>(); See code sample
MIS 324 -- Professor Sandvig 11/9/2018 Generic List<> List<> only collection used in MIS 324 Used in ViewModels Fonts States List<> has methods for sorting, finding, etc. Often use LINQ (Language Integrated Query) to query List
Summary Collections frequently used Generic Collections Strongly typed Generic List<> used for most data lists