简体   繁体   中英

Are functions with more than 1 return types in C# possible?

I know that functions cannot have more than 1 return type per se. I'm searching for alternatives.

Scenario

I have a class Product in which I have various properties. I have a class Discounts , in which I have many other classes (different types of discounts)

Discounts, regardless of the type, are objects - in that they all have a Type (percentage/fixed) and Value (price in decimal format) etc. Each and every discount also has a product assigned to it, so that the system knows which Product to apply this discount rule on.

Three Discount Types:

BasicDiscount AdvancedDiscount SuperDiscount

I have a List<T> for every different discount, so that they're grouped by type.

My aim is to have one function that takes a Product as a parameter, and loops through each discount in the discount lists to check if the product is present. If and when the object is found, I want the function to return that object, whether it's a BasicDiscount , AdvancedDiscount , SuperDiscount etc. (The listed types of discounts would be the ones present in my Discounts class)

This is an example of where I'm currently at.

Suppose Product X has a BasicDiscount applied on it.

function object getDiscount(Product p){

    (UnknownType) discount;

     foreach (BasicDiscount b in BasicDiscounts)
                    if (b.Product.ID == p.ID)
                       discount = b;
     foreach (AdvancedDiscount ad in AdvancedDiscounts)
                    if (ad.Product.ID == p.ID)
                       discount = ad;
     foreach (SuperDiscount sd in SuperDiscounts)
                    if (sd.Product.ID == p.ID)
                       discount = sd;

     return discount;

}

Since I don't know which type of discount the product could be present in, how can I go about returning the correct discount?

Or is there another way?

TLDR; - If you have to make a function in which either the name or the age had to be returned (with either int or string as their data type), but you didn't know which one would be selected - what return type would your function be?

Help is much appreciated, thanks! :)

Interfaces!

You can use an interface that each of your discount classes implement, then you can just return the interface to the calling method.

Eg.

public interface IDiscount
{
    // method or property signatures
}

public class BasicDiscount : IDiscount
{
    // implementation of interface members
}

public IDiscount getDiscount(Product p)
{
     (IDiscount) discount;

     foreach (BasicDiscount b in BasicDiscounts)
         if (b.Product.ID == p.ID)
             discount = b;
     foreach (AdvancedDiscount ad in AdvancedDiscounts)
         if (ad.Product.ID == p.ID)
             discount = ad;
     foreach (SuperDiscount sd in SuperDiscounts)
         if (sd.Product.ID == p.ID)
             discount = sd;

     return discount;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM