Delving Deeper into Interface-Based Programming
(Page 1 of 4 )
In this second article in a four part series, you'll learn about interface methods, properties, and events; how to implement multiple interfaces; and more. It is excerpted from chapter three of
Programming .NET Components, Second Edition, written by Juval Lowy (O'Reilly, 2006; ISBN: 0596007620). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Interface Methods, Properties, and Events
An interface isn’t limited only to defining methods. An interface can also define properties, indexers, and events. Example 3-7 shows the syntax for defining all of these in an interface and the corresponding implementation.
Example 3-7. An interface can define methods, properties, indexers, and events
public delegate void NumberChangedEventHandler(int number);
public interface IMyInterface
{
void Method1(); //A method
int SomeProperty{ get; set; }//A property
int this[int index]{ get; set;}//An indexer
event NumberChangedEventHandler NumberChanged;//An event
}
public class MyClass : IMyInterface
{
public event NumberChangedEventHandler NumberChanged;
public void Method1()
{...}
public int SomeProperty
{
get
{...}
set
{...}
}
public int this[int index]
{
get
{...}
set
{...}
}
}
Interfaces and Structs
An interesting use of interfaces with properties involves structs. In .NET, a struct (a Structure in Visual Basic 2005) can’t have a base struct or a base class, because it’s a value type. However, .NET does permit structs to implement one or more interfaces. The reason for this is that sometimes you want to define abstract data storage, and there are a number of possible implementations for the actual structure. By defining an interface (preferably with properties only, but it can have methods as well), you can pass around the interface instead of the actual struct and gain the benefits of polymorphism, even though structs aren’t allowed to derive from a common base struct. Example 3-8 demonstrates the use of an interface (with properties only) as a base type for structs.
Example 3-8. Using an interface as a base type for structs
public interface IMyBaseStruct
{
int SomeNumber{ get; set; }
string SomeString{ get; set; }
}
struct MyStruct : IMyBaseStruct
{
public int SomeNumber
{ get{...} set{...} }
public string SomeString
{ get{...} set{...} }
//Rest of the implementation
}
struct MyOtherStruct : IMyBaseStruct
{
public int SomeNumber
{ get{...} set{...} }
public string SomeString
{ get{...} set{...} }
//Rest of the implementation
}
//A method that accepts a struct, without knowing exactly the type
public void DoWork(IMyBaseStruct storage)
{...}
Next: Interfaces and Partial Types >>
More .NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter three of Programming .NET Components, Second Edition, written by Juval Lowy (O'Reilly, 2006; ISBN: 0596007620). Check it out today at your favorite bookstore. Buy this book now.
|
|