CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define...

19
CIS 330 1 C# Lesson 13 Interfaces

Transcript of CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define...

Page 1: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 1

C# Lesson 13

Interfaces

Page 2: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 2

Objectives

• Understand the Purpose of Interfaces.

• Define an Interface.

• Use an Interface.

• Implement Interface Inheritance.

Page 3: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 3

Interfaces• An interface looks like a class, but has no

implementation

• Only thing it contains are definitions of events, indexers, methods and/or properties

• interfaces only provide definitions because they are inherited by classes and structs, which must provide an implementation for each interface member defined

Page 4: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 4

Interfaces (cont)• Great for putting together plug-n-play like

architectures where components can be interchanged at will

• The interface forces each component to expose specific public members that will be used in a certain way

• Because interfaces must be defined by inheriting classes and structs, they define a contract

Page 5: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 5

Interfaces (cont)• A common naming convention is to prefix

all interface names with a capital "I".  • Indicating that a class inherits an interface

is the same as inheriting a class. – class InterfaceImplementer : IMyInterface

• Interfaces may also be inherited by other interface

• Any class or struct that inherits an interface must also implement all members in the entire interface inheritance chain

Page 6: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 6

C# Lesson 14

Introduction to Delegates and Events

Page 7: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 7

Objectives

• Understand What a Delegate Is

• Understand What an Event Is

• Implement Delegates

• Fire Events

Page 8: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 8

Delegates• A delegate is a C# language element that allows

you to reference a method • Gives you maximum flexibility to implement any

functionality you want at runtime • Delegate declarations look somewhat like

methods, except they have the delegate modifier, are terminated with a semi-colon (;), and have no implementation – public delegate int Comparer(object obj1, object obj2);

Page 9: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 9

Delegates (cont)• Delegate handler method must conform to

the signature of the Comparer delegate– public static int CompareFirstNames(object name1, object name2)    {        ...    }

• To use a delegate, you must create an instance of it – Comparer cmp = new Comparer(Name.CompareFirstNames);

Page 10: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 10

Delegates (cont)• The delegate, cmp, is then used as a

parameter to the Sort() method, which uses it just like a normal method – sd.Sort(cmp);

• Using this technique, any delegate handler method may be passed to the Sort() method at run-time

Page 11: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 11

Events • Modern GUI programs operate on an

event-based model• A C# event is a class member that is

activated whenever the event it was designed for occurs

• Anyone interested in the event can register and be notified as soon as the event fires

• At the time an event fires, registered methods will be invoked.

Page 12: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 12

Events (cont)• Events and delegates work hand-in-hand

to provide a program's functionality

• A class that declares an event may register one of its methods for the event

• This occurs through a delegate, which specifies the signature of the method that is registered for the event

• Start by Defining delegate– public delegate void StartDelegate();

Page 13: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 13

Events (cont)• Then, event declaration

– public event StartDelegate StartEvent;

• Anyone interested in an event can register by hooking up a delegate for that event – StartEvent += new StartDelegate(OnStartEvent);

• Firing an event – StartEvent();

• hook up an EventHandler delegate to a Button Click event – clickMe.Click += new EventHandler(OnClickMeClicked);

Page 14: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 14

C# Lesson 15

Introduction to Exception Handling

Page 15: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 15

Objectives

• Learn what an exception is

• Implement a routine with a try/catch block

• Release resources in a finally block

Page 16: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 16

Exceptions • Exceptions are unforeseen errors that

happen in your programs • When exceptions occur, they are said to

be "thrown“• What is actually thrown is an object that is

derived from the System.Exception class • The System.Exception class provides

several methods and properties for obtaining information on what went wrong – Message , StackTrace , ToString()

Page 17: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 17

Exceptions

• It's easy to find out what exceptions a method can raise by looking in the .NET Frameworks SDK Documentation

• When exceptions are thrown, you need to be able to handle them.  This is done by implementing a try/catch block

Page 18: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 18

Exceptions Exampleusing System;using System.IO;

class TryCatchDemo{    static void Main(string[] args)    {        try        {         File.OpenRead("NonExistentFile");        }        catch (Exception ex)        {         Console.WriteLine(ex.ToString());        }    }}

Page 19: CIS 3301 C# Lesson 13 Interfaces. CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface.

CIS 330 19

Exceptions (cont)• Exceptions that are not handled will

normally bubble up the stack until a calling routine in the call chain handles them

• If you forget to include try/catch blocks your program will abort

• Sometimes you need to perform clean up actions whether or not your program succeeds, which is what the finally block is for