Download presentation
Presentation is loading. Please wait.
Published byTheodore Newman Modified over 9 years ago
1
CIS 3301 C# Lesson 13 Interfaces
2
CIS 3302 Objectives Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface Inheritance.
3
CIS 3303 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
4
CIS 3304 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
5
CIS 3305 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
6
CIS 3306 C# Lesson 14 Introduction to Delegates and Events
7
CIS 3307 Objectives Understand What a Delegate Is Understand What an Event Is Implement Delegates Fire Events
8
CIS 3308 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);
9
CIS 3309 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);
10
CIS 33010 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
11
CIS 33011 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.
12
CIS 33012 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();
13
CIS 33013 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);
14
CIS 33014 C# Lesson 15 Introduction to Exception Handling
15
CIS 33015 Objectives Learn what an exception is Implement a routine with a try/catch block Release resources in a finally block
16
CIS 33016 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()
17
CIS 33017 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
18
CIS 33018 Exceptions Example using System; using System.IO; class TryCatchDemo { static void Main(string[] args) { try { File.OpenRead("NonExistentFile"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
19
CIS 33019 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.