Skip to Main Content
C# Cookbook, 2nd Edition
book

C# Cookbook, 2nd Edition

by Jay Hilyard, Stephen Teilhet
January 2006
Intermediate to advanced content levelIntermediate to advanced
1184 pages
43h 23m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook, 2nd Edition
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
146
|
Chapter 3: Classes and Structures
public class Personnel<T> : List<T>, IPrint
{
public void Print( )
{
foreach (T obj in this)
{
Console.WriteLine("Person: " + obj);
}
}
}
The following two methods, TestIPrintInterface and CommonPrintMethod, show how
any object that implements the IPrint interface can be passed to the CommonPrintMethod
polymorphically and printed:
public void TestIPrintInterface( )
{
// Create an InventoryItems object and populate it.
IPrint obj = new InventoryItems<string>( );
((InventoryItems<string>)obj).Add("Item1");
((InventoryItems<string>)obj).Add("Item2");
// Print this object.
CommonPrintMethod(obj);
Console.WriteLine( );
// Create a Personnel object and populate it.
obj = new Personnel<string>( );
((Personnel<string>)obj).Add("Person1");
((Personnel<string>)obj).Add("Person2");
// Print this object.
CommonPrintMethod(obj);
}
private void CommonPrintMethod(IPrint obj)
{
Console.WriteLine(obj.ToString( ));
obj.Print( );
}
The output of these methods is shown here:
CSharpRecipes.ClassAndStructs+InventoryItems`1[System.String]
Inventory Item: Item1
Inventory Item: Item2
CSharpRecipes.ClassAndStructs+Personnel`1[System.String]
Person: Person1
Person: Person2
Discussion
The use of interfaces is found throughout the FCL. One example is the IComparer
interface: this ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 0596100639Supplemental ContentCatalog PageErrata