Elegant ways to return multiple values from a function

后端 未结 14 896
梦谈多话
梦谈多话 2020-12-15 03:49

It seems like in most mainstream programming languages, returning multiple values from a function is an extremely awkward thing.

The typical soluti

14条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-15 04:44

    c#

    public struct tStruct
    {
        int x;
        int y;
        string text;
        public tStruct(int nx, int ny, string stext)
        {
             this.x = nx;
             this.y = ny;
             this.text = stext;
        }
    }
    
    public tStruct YourFunction()
    {
        return new tStruct(50, 100, "hello world");
    }
    
    public void YourPrintFunction(string sMessage)
    {
        Console.WriteLine(sMessage);
    }
    

    in Lua you call

    MyVar = YourFunction();
    YourPrintfFunction(MyVar.x);
    YourPrintfFunction(MyVar.y);
    YourPrintfFunction(MyVar.text);
    

    Output: 50 100 hello world

    Paul

提交回复
热议问题