Return multiple values to a method caller

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

    You cannot do this in C#. What you can do is have a out parameter or return your own class (or struct if you want it to be immutable).

    Using out parameter
    public int GetDay(DateTime date, out string name)
    {
      // ...
    }
    
    Using custom class (or struct)
    public DayOfWeek GetDay(DateTime date)
    {
      // ...
    }
    
    public class DayOfWeek
    {
      public int Day { get; set; }
      public string Name { get; set; }
    }
    

提交回复
热议问题