C Collections MIS 324 PROFESSOR SANDVIG Overview What

  • Slides: 14
Download presentation
C# Collections MIS 324 PROFESSOR SANDVIG

C# Collections MIS 324 PROFESSOR SANDVIG

Overview What are collections Arrays Collections Non-generic Generic Language Integrated Query (LINQ) Sort &

Overview What are collections Arrays Collections Non-generic Generic Language Integrated Query (LINQ) Sort & filter collections Similar to SQL

What are Collections store multiple data values Frequently work with collections: Database records List

What are Collections store multiple data values Frequently work with collections: Database records List of products List of people Shopping cart Cookies Web form values Etc.

Why Use Collections? Easy data manipulation Add, Many delete, find, sort, etc. choices: Array

Why Use Collections? Easy data manipulation Add, Many delete, find, sort, etc. choices: Array Queue Stack Dictionary List<> Sorted List Etc.

Collection Choices Arrays C# language feature Simple Limited functionality . NET library namespaces System.

Collection Choices Arrays C# language feature Simple Limited functionality . NET library namespaces System. Collections. Generic

Types of Collections A few types of collections: Collection Best for Hashtable Fast lookups

Types of Collections A few types of collections: Collection Best for Hashtable 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 C# language feature Limited features No sorting No filtering Size

Array Simple collection type C# language feature Limited features No sorting No filtering Size defined when initialized

Array Advantages Simple Basic features: Iterate, retrieve values, find value Disadvantages Size fixed when

Array Advantages Simple Basic features: Iterate, retrieve values, find value Disadvantages Size fixed when initialized Minimal features Inefficient Stores data as type “object” Data must be unboxed and boxed

Collections Namespaces . NET library namespaces System. Collections. Generic Provide classes for managing data

Collections Namespaces . NET library namespaces System. Collections. Generic Provide classes for managing data Lookup (fast) Sort Filter Find Etc.

Generic Collections System. Collections. Generic namespace Strongly typed! Faster Used extensively in MVC Retain

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”? Items in collection can be any “generic” object Declare type

Generic Collections Why “generic”? Items in collection can be any “generic” object Declare type when create collection List<int> my. Generic. List = new List<int>(); Generics can hold any object List<Person. Model> person. Model = new List<Person. Model>(); See code sample

Generic List<> only collection used in MIS 324 Used in View. Models Fonts States

Generic List<> only collection used in MIS 324 Used in View. Models Fonts States List<> has methods for sorting, finding, etc. Often use LINQ (Language Integrated Query) to query List

LINQ Language Integrated Query language built into C# Use with any collection type Including

LINQ Language Integrated Query language built into C# Use with any collection type Including database Capabilities similar to SQL Uses Lamda Syntax . where(collection => expression) Collection Examples

Summary Collections frequently used Generic Collections Strongly typed Generic List<> used for most data

Summary Collections frequently used Generic Collections Strongly typed Generic List<> used for most data lists