Which two way I can use to make a method return a int and string.
1
Expert's answer
2017-09-08T11:18:07-0400
/*first way is to use out parameters*/ public void GetStringAndInt(out int intVar, out string stringVar) { intVar = 123; stringVar = "abc"; }
/*second way is a function that returns your own class*/ public IntAndString GetStringAndInt() { var intAndString = new IntAndString(); intAndString.intVal = 123; intAndString.stringVal = "abc"; return intAndString; }
public class IntAndString { public int intVal { get; set; } public string stringVal { get; set; } }
/*third way - use a Tuple class (availibale from 4.0 framework)*/ public Tuple<int, string> GetStringAndInt() { return Tuple.Create(123, "abc"); }
Comments
Leave a comment