Presentation is loading. Please wait.

Presentation is loading. Please wait.

Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {

Similar presentations


Presentation on theme: "Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {"— Presentation transcript:

1 Neal Stublen nstublen@jccc.edu

2

3 What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) { myArray[index] = 3; }  The [ ] is the indexer.

4 What’s an indexer?  An indexer accesses one value by using another value  A special property: public this[ ]  The index_type is often an integer, but can be another data type  Implemented on collection classes  Throws an ArgumentException ArgumentNullException ArgumentOutOfRangeException

5 Indexer Example class MyThing { } class MyCollection { public void AddThing(MyThing inThing) {... } public MyThing this[int index] { get {... } set {... } }

6

7 Start with an Example

8 What’s a delegate?  A delegate specifes a method signature for an event  A special data type: public delegate DelegateName([parameters_list]);

9 Delegate Example // The delegate type declaration public delegate void NameChangedEventHandler(object sender, EventArgs e); class NameObject { // The event member public event NameChangedEventHandler NameChanged; public void setName(string inName) { mName = inName; if (NameChanged != null) { NameChanged(this); }

10 Connecting to an Event class SomeObject { private NameObject mTheName = new NameObject(); public void Init() { mTheName.NameChanged += new NameChangedEventHandler(EventHandler) } private void EventHandler(object sender) { }

11

12 What operations?  Unary operators +, -, !, ++, --, true, false public static operator ( )  Binary operators +, -, *, /, %, &, |, ==, !=, >, =, <= public static operator (, )

13 Operator Example class MyCollection { private List mList; public static MyCollection operator + (MyCollection col, object newObject) { col.mList.Add(newObject); return col; } collection += new object();

14 Overloading ==  Must also overload Equals()  Must also overload GetHashCode()  Must also overload !=  Must overload relational operators in pairs ( ), ( =), (==, !=)

15 Practice Exercise  Exercise 13-1, p. 418

16

17 What is inheritance?  One class inherits the attributes and behaviors of another class  The base class should be a larger classification of the derived class (ex. a Control is a larger classification for Button) A Button “is-a” Control; a Button “has-a” BackColor A Book “is-a” Product; a Book “has-a” Publisher  The derived class extends or overrides behavior

18 .NET Inheritance  All classes implicitly inherit from System.Object GetType() ToString() Equals() ReferenceEquals() GetHashCode() Finalize() MemberwiseClone()

19

20 How does inheritance work? class Fourthclass Thirdclass Secondclass First methodA methodB methodC methodD methodE Fourth fourth = new Fourth(); fourth.methodA(); fourth.methodB(); fourth.methodC(); fourth.methodD(); fourth.methodE(); First first = new Fourth(); first.methodA(); first.methodB(); first.methodC(); first.methodD(); first.methodE(); “Polymorphism” The base class can take many different forms.

21 Polymorphism List myList = new List (); myList.Add(new First()); myList.Add(new Second()); myList.Add(new Third()); myList.Add(new Fourth()); foreach (First item in myList) { item.methodB(); item.methodC(); }

22 How do we “do” inheritance?  Keywords: virtual, override  A base class declares a method as virtual  A derived class declares a method as override  public/private => public/private/protected/internal  Reference base class using “base.”

23 Inherited Classes class First { public virtual void methodA() { } class Second : First { public override void methodA() { base.methodA(); }

24 Considerations for Inheritance  Confirm “is-a” versus “has-a” relationship  Does adding one or more properties make sense?  Would an interface be more beneficial?  Implicit and explicit casting between inherited types  Using the “as” operator instead of casting to avoid exceptions

25 Abstract Classes  Abstract classes cannot be instantiated, and can only serve as a base class  Abstract methods and properties must be overridden in a derived class You know the method or property exists for every object of this type, but there is no implementation at this level of abstraction.

26 Sealed Classes  Sealed classes cannot be inherited  Sealed methods and properties cannot be overridden

27 Start thinking about how objects you need to model may inherit from one another. Are there any obvious heirarchies, common attributes, or shared behavior?

28 Suggestions  Try to work through Exercises 14-1 and 14-2 in the book (p. 457)


Download ppt "Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {"

Similar presentations


Ads by Google