[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Implement an interface in C#

example

Before you can implement an interface, you need to know what an interface is. An interface defines properties, methods, and events that an object must provide to satisfy the interface without providing any implementation of those features. If a class implements an interface, then the program knows that objects from that class provide the defined features. That means the program can use those features even if it doesn't know all of the details of the class.

For example, the IComparable interface requires that a class provide a CompareTo method that compares an instance of the class to another object. If a class implements the IComparable interface in a class, then the program knows it can use the CompareTo method of objects from that class.

This also allows .NET Framework methods to use an object's CompareTo method. For example, the Array.Sort method can use that method to sort an array of those objects.

To indicate that a class implements an interface in the class's declaration, follow the class name by a colon and the interface's name as in the following.

class SortablePerson : IComparable { ... }

example After you declare the class, you need to provide the methods that the interface requires. You can type those in from memory, but there's a much easier way. Click on the interface's name and look for a little auto-correct bar as shown in Figure 1.
example Hover over the auto-correct bar to display a dropdown arrow. Click the arrow and select the first command as shown in Figure 2.

At that point, Visual Studio fills in empty methods required by the interface. For the IComparable interface, Visual Studio adds the following code to the class:

#region IComparable Members public int CompareTo(object obj) { throw new NotImplementedException(); } #endregion

Now you can fill in whatever code is necessary to make the methods work. This example uses the following SortablePerson class.

public class SortablePerson : IComparable { public string FirstName, LastName; public override string ToString() { return LastName + ", " + FirstName; } public int CompareTo(object obj) { SortablePerson other = obj as SortablePerson; return this.ToString().CompareTo(other.ToString()); } }

This class has simple FirstName and LastName fields. It overrides its ToString method to return the person's name as in "Stephens, Rod."

The implementation of the CompareTo method compares the ToString values of the SortablePerson and another.

The following code shows how the example makes an array of SortablePersons, sorts it, and displays the result in a ListBox.

// Make some people, sort them, and display them. SortablePerson[] sorted = { new SortablePerson() { FirstName = "Sam", LastName = "Cart" }, new SortablePerson() { FirstName = "Ann", LastName = "Beech" }, new SortablePerson() { FirstName = "Mark", LastName = "Ash" }, new SortablePerson() { FirstName = "Chris", LastName = "Beech" }, new SortablePerson() { FirstName = "Phred", LastName = "Cart" }, }; Array.Sort(sorted); lstSorted.DataSource = sorted;

IComparable is one of the more useful interfaces defined by the .NET Framework. You can also define your own interfaces if you like.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.