c# data structures and generics by michael and miles

15
C# Data Structures AND Generics By Michael and Miles

Upload: doris-norris

Post on 18-Dec-2015

222 views

Category:

Documents


4 download

TRANSCRIPT

C# Data Structures AND Generics

By Michael and Miles

Overview

What are data structures?Benefits and uses

What are generics?Benefits and uses

Live samplesSummary

Data Structures1. Collections of data 2. Fetch and store 3. type of data structure depends on situation4. Examples

Array, arraylist, list, linkedlist, stack, queue, dictionary, etc.

5. Most basic is the array

Arrays1. Most commonly used data structure2. Contents are stored in contiguous

memory3. All data must be same typePros:Quick direct access (for loop)Easy to useCons:Searching is proportional to data sizeCannot resize array

What are generics?

1. One of the most powerful features introduced in C# 2.0

2. System.Collections.Generic…3. Mainly used with collections/data

structures4. Type safety5. Generic methods

1. Similar operations on different data types

1. Pros• Type safety• Reduces the need for type casting – less run-time errors

• Performance boost – No boxing/unboxing (less code)

• maximize code reuse2. Cons• Can get complex• Learning curve

List<T>1. Similar to arrays – 2. A generic data structure3. Type-safe4. Dynamic resizing

- .Add() .Insert() .Remove()5. Built in search methods6. flexibility

Like a circular arrayFirst come first serve

Insertions made at the tailPros:

Good for processing Cons:

Not searchable Not many uses

Queues

Dictionaries

Key value pairsData is unorderedEasy lookupsType safe (generic)

Drawbacks of non-generic

data structuresData stored as type object– Casting: conversion of data types

double x = 1234.7;int a; a = (int)x; // cast double to int

– Limited type checking *

Increased Overhead– More work to getting something done

“CANNOT IMPLICITLY CONVERT TYPE OBJECT TO INT”

And then there’s LINQWrite queries (similar to SQL queries)Retrieve info from many data structures– Arrays, List<> etc.

Quick examplehttp://msdn.microsoft.com/en-us/library/bb907066.aspx

Summary

Types of StructuresGenerics –What they are/how to use

Practical uses

Questions/comments/

compliments?