Tuple or out parameter for multiple return values?

I want to return some parameters from a method in C #. I just wanted to know which one is better or Tuple?

static void Split (string name, out string firstNames, out string lastName) { int i = name.LastIndexOf (' '); firstNames = name.Substring (0, i); lastName = name.Substring (i + 1); } static Tuple<string,string> Split (string name) { //TODO } 
+6
source share
1 answer

Typically, a class (value) is hidden if you need to return more than one value from a method. How about a class of values ​​using the Split() method as ctor:

 public class Name { public Name(string name) { int i = name.LastIndexOf (' '); FirstNames = name.Substring (0, i); LastName = name.Substring (i + 1); } public string FirstName {get; private set;} public string LastName {get; private set;} } 

Instead

 Split(name, out string firstName, out string lastName); 

simply

 Name n = new Name(name); 

and access the first and last name through n.FirstName and n.LastName .

+4
source

Source: https://habr.com/ru/post/885845/


All Articles