Is it possible for a function to return two values?

后端 未结 13 1536
谎友^
谎友^ 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: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).

提交回复
热议问题