Is it possible for a function to return two values?

后端 未结 13 1483
谎友^
谎友^ 2020-12-31 08:41

Is it possible for a function to return two values? Array is possible if the two values are both the same type, but how do you return two different type values?

相关标签:
13条回答
  • 2020-12-31 09:37

    To return 2 values I usually use Pair class from http://blog.o-x-t.com/2007/07/16/generic-pair-net-class/. If you need to return from method 2 values that describe the range, e.g. From/To or Min/Max, you can use FromToRange class.

    public class FromToRange<T>
        {
            public T From { get; set; }
            public T To { get; set; }
    
            public FromToRange()
            {
            }
    
            public FromToRange(T  from, T  to)
            {
                this.From = from;
                this.To = to;
            }
            public override string ToString()
            {
                string sRet = String.Format("From {0} to {1}", From, To);
                return sRet;
            }
            public override bool Equals(object obj)
            {
                if (this == obj) return true;
                FromToRange<T> pair = obj as FromToRange<T>;
                if (pair == null) return false;
                return Equals(From, pair.From) && Equals(To, pair.To);
            }
    
            public override int GetHashCode()
            {
                return (From != null ? From.GetHashCode() : 0) + 29 * (To != null ? To.GetHashCode() : 0);
            }
    
        }
    
    0 讨论(0)
  • 2020-12-31 09:38

    In a word, no.

    But you can define a struct (or class, for that matter) for this:

    struct TwoParameters {
        public double Parameter1 { get; private set; }
        public double Parameter2 { get; private set; }
    
        public TwoParameters(double param1, double param2) {
            Parameter1 = param1;
            Parameter2 = param2;
        }
    }
    

    This of course is way too specific to a single problem. A more flexible approach would be to define a generic struct like Tuple<T1, T2> (as JaredPar suggested):

    struct Tuple<T1, T2> {
        public T1 Property1 { get; private set; }
        public T2 Property2 { get; private set; }
    
        public Tuple(T1 prop1, T2 prop2) {
            Property1 = prop1;
            Property2 = prop2;
        }
    }
    

    (Note that something very much like the above is actually a part of .NET in 4.0 and higher, apparently.)

    Then you might have some method that looks like this:

    public Tuple<double, int> GetPriceAndVolume() {
        double price;
        int volume;
    
        // calculate price and volume
    
        return new Tuple<double, int>(price, volume);
    }
    

    And code like this:

    var priceAndVolume = GetPriceAndVolume();
    double price = priceAndVolume.Property1;
    int volume = priceAndVolume.Property2;
    
    0 讨论(0)
  • 2020-12-31 09:38

    It is not directly possible. You need to return a single parameter that wraps the two parameters, or use out parameters:

    object Method(out object secondResult)
    {
        //...
    

    Or:

    KeyValuePair<object,object> Method()
    {
       // ..
    
    0 讨论(0)
  • 2020-12-31 09:38

    You could use the out parameter.

    int maxAge;
    int minAge;
    public int GetMaxAgeAndMinAge(out int maxAge, out int minAge)
    {
        MaxAge = 60;
        MinAge = 0;
        return 1; //irrelevant for this example since we care about the values we pass in
    }
    

    I really tend to stay away from this, I think that it is a code-smell. It works for quick and dirty though. A more testable and better approach would be to pass an object that represents your domain (the need to see two these two values).

    0 讨论(0)
  • 2020-12-31 09:42

    you can try this

    public IEnumerable<string> Get()
     {
         return new string[] { "value1", "value2" };
     }
    
    0 讨论(0)
  • 2020-12-31 09:45

    It is not possible to return more than one value from a function, unless you are returning a type that contains multiple values in it (Struct, Dictionary, etc). The only other way would be to use the "out" or "ref" keywords on the incoming parameters.

    0 讨论(0)
提交回复
热议问题