Problem with if block using interfaces
We came up with a weird error that I've never seen before today. Here's a customer class and an interface to a strategy class (ICustomerStrategy) with two concrete implementations (GoodCustomer, BadCustomer):
1: public class Customer
2: {
3: private readonly int _creditLimit;
4:
5: public Customer(int creditLimit)
6: {
7: _creditLimit = creditLimit;
8: }
9:
10: public int CreditLimit
11: {
12: get { return _creditLimit; }
13: }
14: }
15:
16: public interface ICustomerStrategy
17: {
18: }
19:
20: internal class BadCustomer : ICustomerStrategy
21: {
22: }
23:
24: internal class GoodCustomer : ICustomerStrategy
25: {
26: }
In the consumer class we have a method called GetStrategy that returns an ICustomerStrategy object.
This statement using an if-block works fine:
1: public class Strategy
2: {
3: public ICustomerStrategy GetStrategy(Customer customer)
4: {
5: if (hasGoodCredit(customer))
6: {
7: return new GoodCustomer();
8: }
9:
10: return new BadCustomer();
11: }
12:
13: private static bool hasGoodCredit(Customer customer)
14: {
15: return customer.CreditLimit > 1000;
16: }
17: }
However this statement using the conditional operator doesn't:
1: public class Strategy
2: {
3: public ICustomerStrategy GetStrategy(Customer customer)
4: {
5: return hasGoodCredit(customer) ? new GoodCustomer() : new BadCustomer();
6: }
7:
8: private static bool hasGoodCredit(Customer customer)
9: {
10: return customer.CreditLimit > 1000;
11: }
12: }
The only way to make it work (thanks ReSharper for the tip!) is to cast the first object to the interface like so:
1: public ICustomerStrategy GetStrategy(Customer customer)
2: {
3: return hasGoodCredit(customer) ? (ICustomerStrategy) new GoodCustomer() : new BadCustomer();
4: }
You can also make it work by using a base abstract class rather than an interface, but I prefer the interface driven approach.
So the question on my noodle is... why?