Created
July 17, 2013 08:29
-
-
Save jhominal/6018805 to your computer and use it in GitHub Desktop.
Gist for demonstration of Jodrell's answer to http://stackoverflow.com/questions/17681197/an-elegant-way-to-do-this-may-be-generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Transaction {} | |
class ServiceCharge : Transaction {} | |
class Sale : Transaction {} | |
interface IChargable<out T> where T : Transaction | |
{ | |
Transaction Charge(); | |
} | |
class BusinessService : IChargable<ServiceCharge> | |
{ | |
public ServiceCharge Charge() | |
{ | |
return new ServiceCharge(); | |
} | |
} | |
class Product : IChargable<Sale> | |
{ | |
public Sale Charge() | |
{ | |
return new Sale(); | |
} | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
var chargables = new IChargable<Transaction>[] | |
{ | |
new BusinessService(), | |
new Product() | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment