Return multiple values to a method caller

前端 未结 26 2325
故里飘歌
故里飘歌 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:37

    There is many way; but if you don't want to create a new Object or structure or something like this you can do like below after C# 7.0 :

     (string firstName, string lastName) GetName(string myParameter)
        {
            var firstName = myParameter;
            var lastName = myParameter + " something";
            return (firstName, lastName);
        }
    
        void DoSomethingWithNames()
        {
            var (firstName, lastName) = GetName("myname");
    
        }
    

提交回复
热议问题