How do you return two values from a single method?

后端 未结 22 859
谎友^
谎友^ 2020-12-11 00:58

When your in a situation where you need to return two things in a single method, what is the best approach?

I understand the philosophy that a method should do one t

22条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 01:57

    I would create data transfer objects. If it is a group of information (first and last name) I would make a Name class and return that. #4 is the way to go. It seems like more work up front (which it is), but makes it up in clarity later.

    If it is a list of records (rows in a database) I would return a Collection of some sort.

    I would never use globals unless the app is trivial.

提交回复
热议问题