Download presentation
Presentation is loading. Please wait.
1
Introduction to .NET Florin Olariu
“Alexandru Ioan Cuza”, University of Iași Department of Computer Science
2
Agenda OOP in .NET Core Arrays Generic list What’s Next
Demo Generic list What’s Next Interview questions
3
OOP in .NET Core Inheritance Inheritance Classes may derive from exiting classes. Existing classes are called : parent class. => and it is a generalization of the exiting class. Inheritance creates an “is –a ” relationship Example: Employee<=Manager, Architect, Technical Lead, Developer
4
OOP in .NET Core – inheritance
Derived class indicates base class with colon. Notes: classes can contain properties and fields (built-in like int, string or custom like other classes or struct’s) Fields are used to support private methods Properties are used to expose functionality to other classes
5
OOP in .NET Core - inheritance
Identify : properties, methods/behavior
6
OOP in .NET Core - polymorphism
The possibility of taking many forms. Method overriding -> modify a method in the derived class : C# in order to be able to use method overriding we need to know: virtual override. Virtual exists in base class, override in derived class.
7
OOP in .NET Core - polymorphism
8
OOP in .NET Core - polymorphism
Another important aspect for polymorphism is “method overloading”. Means: we can have in the same class methods with the same name but different number of parameters.
9
OOP in .NET Core - encapsulation
Encapsulation : a class should have a single responsibility. => don’t forget here, there is another set of principles : SOLID. Within the class we should have private functionality, that should not be exposed and only a : few well-defined methods that are public. Question: Why the method should be public?
10
OOP in .NET Core Inheritance Polymorphism Encapsulation
There are 3 pillars in OOP. => Inheritance, polymorphism and encapsulation.
11
OOP in .NET Core "In the one and only true way. The object-oriented version of 'Spaghetti code' is, of course, …."
12
OOP in .NET Core "In the one and only true way. The object-oriented version of 'Spaghetti code' is, of course, 'Lasagna code'." - Roberto Waltman.
13
Arrays
14
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
15
Arrays Definition
16
Arrays Definition Is a fixed-size list of elements that can be accessed using a positional index number
17
Arrays Red “Salt” 2.21 White “Pepper” 3.43 Definition “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)
18
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”.
19
Arrays Definition Samples Declaring and populating an array
20
Arrays Red White Definition Green Declaring and populating an array
Samples Declaring and populating an array Declaring an array: string[] colors; Red White Green Blue
21
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.
22
Arrays Definition Declaring and populating an array Samples
23
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.
24
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
25
Arrays Demo
26
Generic List
27
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
28
Generic List Definition
It is a strongly typed list of elements that is accessed using a positional index number.
29
Generic List Arrays vs Generic List
30
Generic List Arrays vs Generic List Arrays Generic List
31
Generic List Arrays vs Generic List Arrays Generic List Strongly typed
32
Generic List Arrays vs Generic List Arrays Generic List Strongly typed
Fixed length Expandable
33
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
34
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
35
Generic List Declaring and populating Generic Lists
36
Generic List Declaring and populating Generic Lists
37
Generic List Declaring and populating Generic Lists
This is using Collection initializer.
38
Generic List Frequently asked questions
When is appropriate to use a generic list?
39
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?
40
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
41
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 ( )
42
Generic List Demo
43
What’s next … Generic dictionaries Generic collection interfaces LINQ
44
Interview questions Abstract classes vs Interfaces Boxing/Unboxing
Immutable string
45
One more thing… "Walking on water and developing software from a specification are easy…"
46
One more thing… "Walking on water and developing software from a specification are easy if both are frozen." - Edward V Berard
47
Questions Do you have any other questions?
48
Thanks! See you next time!
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.