Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to .NET Florin Olariu

Similar presentations


Presentation on theme: "Introduction to .NET Florin Olariu"— Presentation transcript:

1 Introduction to .NET Florin Olariu
“Alexandru Ioan Cuza”, University of Iași Department of Computer Science

2 Agenda Arrays Demo Generic list What’s Next Interview questions

3 Arrays

4 Arrays Definition Declaring and populating an array
Samples Declaring and populating an array Using collection initializers Retrieving an element from an array Iterating an array Using array methods Best practices Demo

5 Arrays Definition

6 Arrays Definition Is a fixed-size list of elements that can be accessed using a positional index number

7 Arrays “Salt” 2.21 Red Definition White “Pepper” 3.43 “Onion” 5.01
Is a fixed-size list of elements that can be accessed using a positional index number Sample: Red White Green Blue “Salt” 2.21 “Pepper” 3.43 “Onion” 5.01 “Garlic” 3.20 Notes: 1. In arrays index is “0-based”. 2. Arrays are fixed size.(we will define the size of an array when this is initialized – once is initialized the size cannot be adjusted)

8 Arrays Definition 1 2 3 Red White Green Blue 1 2 3 “Salt” 2.21
Is a fixed-size list of elements that can be accessed using a positional index number Sample: 1 2 3 Red White Green Blue 1 2 3 “Salt” 2.21 “Pepper” 3.43 “Onion” 5.01 “Garlic” 3.20 Notes: In arrays index is “0-based”.

9 Arrays Definition Samples Declaring and populating an array

10 Arrays Red Definition White Declaring and populating an array Green
Samples Declaring and populating an array Declaring an array: string[] colors; Red White Green Blue

11 Arrays Red White Green Blue Definition
Samples Declaring and populating an array Declaring an array: Initializing an array: string[] colors; string[] colors; colors = new string[4]; string[] colors = new string[4]; var colors = new string[4]; Red White Green Blue An array is a reference type. In this case we should use: “new” keyword for initialization.

12 Arrays Definition Declaring and populating an array Samples

13 Arrays Definition Declaring and populating an array Samples
var colors = new string[4]; colors[0] = “Red”; colors[1] = “White”; colors[2] = “Green”; colors[3] = “Blue”; An array is a reference type. In this case we should use: “new” keyword for initialization.

14 Arrays Best practices Do Avoid
Use arrays when the size is known at the design time Do not use arrays when the data comes from a database call For an array name use ‘pluralization’ => Colors

15 Arrays Demo Working with arrays
Note: How to create a R# template for tests

16 Generic List

17 Generic List Definition Arrays vs Generic List
Declaring and populating Generic Lists Using initializers Retrieving elements from Generic lists Iterating through a Generic List Demo

18 Generic List Definition
It is a strongly typed list of elements that is accessed using a positional index number.

19 Generic List Arrays vs Generic List

20 Generic List Arrays vs Generic List Arrays Generic List

21 Generic List Arrays vs Generic List Arrays Generic List Strongly typed

22 Generic List Arrays vs Generic List Arrays Generic List Strongly typed
Fixed length Expandable

23 Generic List Arrays vs Generic List Arrays Generic List Strongly typed
Fixed length Expandable The is no ability to add/remove elements Can add, insert or remove elements

24 Generic List Arrays vs Generic List Arrays Generic List Strongly typed
Fixed length Expandable The is no ability to add/remove elements Can add, insert or remove elements Multi-dimensional One-dimensional

25 Generic List Declaring and populating Generic Lists

26 Generic List Declaring and populating Generic Lists
var cities = new List<string>(); cities.Add(" London"); cities.Add(" Paris"); cities.Add(" Milan");

27 Generic List Declaring and populating Generic Lists
var cities = new List<string>{“London”, “Paris”, “Milan”}; This is using Collection initializer.

28 Generic List Frequently asked questions
When is appropriate to use a generic list?

29 Generic List Frequently asked questions
When is appropriate to use a generic list? Any time the application needs to manage a list of things What are the key differences between an array and a generic list?

30 Generic List Frequently asked questions
When is appropriate to use a generic list? Any time the application needs to manage a list of things What are the key differences between an array and a generic list? An array is fixed length and can have multiple dimensions A generic list can have any length and provides methods to add, insert or remove elements Execution time

31 Generic List Frequently asked questions
When is appropriate to use a generic list? Any time the application needs to manage a list of things What are the key difference between an array and a generic list? An array is fixed length and can have multiple dimensions A generic list can have any length and provides methods to add, insert or remove elements Execution time For this samples I used: List<int> list = new List<int>( ); The second number is a checksum to verify they all did the same work. static class Program { static void Main() Random rand = new Random(12345); for (int i = 0; i < ; i++) list.Add(rand.Next(5000)); } int[] arr = list.ToArray(); int chk = 0; Stopwatch watch = Stopwatch.StartNew(); for (int rpt = 0; rpt < 100; rpt++) int len = list.Count; for (int i = 0; i < len; i++) chk += list[i]; watch.Stop(); Console.WriteLine("List/for: {0}ms ({1})", watch.ElapsedMilliseconds, chk); chk = 0; watch = Stopwatch.StartNew(); for (int i = 0; i < arr.Length; i++) chk += arr[i]; Console.WriteLine("Array/for: {0}ms ({1})", watch.ElapsedMilliseconds, chk); foreach (int i in list) chk += i; Console.WriteLine("List/foreach: {0}ms ({1})", watch.ElapsedMilliseconds, chk); foreach (int i in arr) Console.WriteLine("Array/foreach: {0}ms ({1})", watch.ElapsedMilliseconds, chk); Console.ReadLine(); List/for: 1971ms ( ) Array/for: 1864ms ( ) List/foreach: 3054ms ( ) Array/foreach: 1860ms ( )

32 Generic List Demo Initialization, Add, Insert, Remove, RemoveAt;

33 What’s next … Generic dictionaries Generic collection interfaces LINQ

34 Interview questions Abstract classes vs Interfaces Boxing/Unboxing
Immutable string

35 One more thing…  "Walking on water and developing software from a specification are easy…"

36 One more thing…   "Walking on water and developing software from a specification are easy if both are frozen." - Edward V Berard

37 Questions Do you have any other questions?

38 Thanks! See you next time! 


Download ppt "Introduction to .NET Florin Olariu"

Similar presentations


Ads by Google