I use asp.net 4 and c#.
I would like to know if could be possible return more than one return type in a Method.
As example in this method I return only a single bool type.
protected bool IsFilePresent()
{
return File.Exists(Server.MapPath("/myFile.txt"));
}
But lets imagine I would like return also a string type using in the same method like:
string lineBase = File.ReadAllText(Server.MapPath("/myFile.txt"));
Is possible to do it?
I would appreciate some code example. Many Thanks.
No, it’s not possible to return multiple, different values. You have a few alternatives:
One is to make a
classorstructthat has each of the things you want to return as properties or fields.Another is to use
outparameters, where you pass in a variable to a method, which the method then assigns a value to.refparameters (pass by reference), which is similar toout, but the variable you pass in needs to have been assigned before you call the method (i.e. the method changes the value).In your case, as cjk points out, you could just have the method return a string with the file contents, or null to indicate that the file didn’t exist.