Return multiple values to a method caller

前端 未结 26 2302
故里飘歌
故里飘歌 2020-11-21 21:57

I read the C++ version of this question but didn\'t really understand it.

Can someone please explain clearly if it can be done and how?

26条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 22:45

    No, you can't return multiple values from a function in C# (for versions lower than C# 7), at least not in the way you can do it in Python.

    However, there are a couple alternatives:

    You can return an array of type object with the multiple values you want in it.

    private object[] DoSomething()
    {
        return new [] { 'value1', 'value2', 3 };
    }
    

    You can use out parameters.

    private string DoSomething(out string outparam1, out int outparam2)
    {
        outparam1 = 'value2';
        outparam2 = 3;
        return 'value1';
    }
    

提交回复
热议问题