Introduction to .NET Florin Olariu “Alexandru Ioan Cuza”, University of Iași Department of Computer Science
Agenda OOP in .NET Core Arrays Generic list What’s Next Demo Generic list What’s Next Interview questions
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
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
OOP in .NET Core - inheritance Identify : properties, methods/behavior
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.
OOP in .NET Core - polymorphism
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.
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?
OOP in .NET Core Inheritance Polymorphism Encapsulation There are 3 pillars in OOP. => Inheritance, polymorphism and encapsulation.
OOP in .NET Core "In the one and only true way. The object-oriented version of 'Spaghetti code' is, of course, …."
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.
Arrays
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
Arrays Definition
Arrays Definition Is a fixed-size list of elements that can be accessed using a positional index number
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)
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”.
Arrays Definition Samples Declaring and populating an array
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
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.
Arrays Definition Declaring and populating an array Samples
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.
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
Arrays Demo
Generic List
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
Generic List Definition It is a strongly typed list of elements that is accessed using a positional index number.
Generic List Arrays vs Generic List
Generic List Arrays vs Generic List Arrays Generic List
Generic List Arrays vs Generic List Arrays Generic List Strongly typed
Generic List Arrays vs Generic List Arrays Generic List Strongly typed Fixed length Expandable
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
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
Generic List Declaring and populating Generic Lists
Generic List Declaring and populating Generic Lists
Generic List Declaring and populating Generic Lists This is using Collection initializer.
Generic List Frequently asked questions When is appropriate to use a generic list?
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?
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
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>(6000000); 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 < 6000000; 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 (589725196) Array/for: 1864ms (589725196) List/foreach: 3054ms (589725196) Array/foreach: 1860ms (589725196)
Generic List Demo
What’s next … Generic dictionaries Generic collection interfaces LINQ
Interview questions Abstract classes vs Interfaces Boxing/Unboxing Immutable string
One more thing… "Walking on water and developing software from a specification are easy…"
One more thing… "Walking on water and developing software from a specification are easy if both are frozen." - Edward V Berard
Questions Do you have any other questions?
Thanks! See you next time!