Answer to Question #69985 in C# for Omar

Question #69985
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");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS