Presentation is loading. Please wait.

Presentation is loading. Please wait.

Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc.Slide 1.

Similar presentations


Presentation on theme: "Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc.Slide 1."— Presentation transcript:

1 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc.Slide 1

2 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 2

3 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 3

4 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 4 A simple interface. No access modifier No code in the method

5 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 5

6 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 6 product is a Product class and also IDisplayable object Errors for not creating methods in subclasses

7 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 7

8 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 8

9 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 9

10 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 10 Project  Add New Item  Interface End with semicolon No access modifiers All are abstract & public

11 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 11

12 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 12

13 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 13 1 2 3 4

14 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 14 Create class Type interface name Red underline warning Use drop down at light bulb

15 Implement interface Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 15

16 Explicit implementation Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 16

17 Simple explicit implementation example Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 17

18 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 18

19 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 19

20 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 20 Change code and description Different code and description Same price

21 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 21

22 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 22

23 Generics Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 23.NET has List<>, SortedList<>, Stack<>, Queue<> Create your own generic collections Defer the specification of one or more types in a class Wait until a class or method is declared and instantiated before knowing the actual type Avoid “boxing” and “unboxing” overhead –Casting from one data type to another

24 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 24 T is the type, but any letter or word will work We can pass any object to this class and it will add it to the list. Make a list containing T types

25 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 25 Override ToString method to display all items in the list

26 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 26 List 1 - ints 11 7 Console.WriteLine("List 1 - ints"); CustomList list1 = new CustomList (); int i1 = 11; int i2 = 7; list1.Add(i1); list1.Add(i2); Console.WriteLine(list1.ToString()); Test 1 of the CustomList<> class Make CustomList of integers

27 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 27 List 2 - Products VB12 Murach's Visual Basic 2012 $54.50 CS12 Murach's C# 2012 $54.50 Console.WriteLine("List 2 - Products"); CustomList list2 = new CustomList (); Product p1 = new Product("VB12", "Murach's Visual Basic 2012", 54.50m); Product p2 = new Product("CS12", "Murach's C# 2012", 54.50m); list2.Add(p1); list2.Add(p2); Console.Write(list2.ToString()); Test 2 of the CustomList<> class ToString method adds tabs in this version of CustomList

28 Generic.NET interface Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 28 Interfaces for generic collections

29 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 29 Interfaces for generic collections

30 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 30 object1.property.CompareTo(object2.property)

31 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 31 Current object is THIS Compare object is passed to method

32 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 32 P1.CompareTo(P2) P1 is current object P2 is compare object

33 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 33

34 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 34 More code is needed here Item could be a null

35 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 35

36 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 36 Regular IEnumerable interface Generic IEnumerable interface

37 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 37

38 Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 38

39 Implementing the Interface Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 39 Code stubs only

40 Conclusion Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc. Slide 40 Interfaces –Create –Implement Generics –Implementing generics Constraints


Download ppt "Murach's C# 2012, C15© 2013, Mike Murach & Associates, Inc.Slide 1."

Similar presentations


Ads by Google