简体   繁体   中英

Is possible return more than one return type in a Method?

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:

  1. One is to make a class or struct that has each of the things you want to return as properties or fields.

  2. Another is to use out parameters, where you pass in a variable to a method, which the method then assigns a value to.

  3. You could also use ref parameters (pass by reference), which is similar to out , but the variable you pass in needs to have been assigned before you call the method (ie 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.

For this example, you have 3 choices.

  1. Add an out parameter to your method and set it in the body (code below)
  2. Create a custom class that contains a Booelan and a String and return that
  3. Just return the string, but return null when the file does not exist

protected bool IsFilePresent(out String allText) 
{ 
     Boolean fileExists = File.Exists(Server.MapPath("/myFile.txt")); 
     if (fileExists)
     {
         allText = File.ReadAllText(Server.MapPath("/myFile.txt")); 
     }
     return fileExists;
} 

Yes you can only if your using framework 4. Look into tuple

http://sankarsan.wordpress.com/2009/11/29/tuple-in-c-4-0/

It's very easy nowadays... use Tuple

public (string,int) MyMethod(){
   return ("text", 3);
}   

string text;
int number;
(text, number) = MyMethod();

//OR
(var text, var number) = MyMethod();

You can return a class with different types in it :

class TestFile
{
    private bool isFilePresent;
    private string lineBase;
}

Yes, you can create a wrapper class called (for instance) "Pair" parametrized with two types, put two values in the object of that class and return it as a result. In your case it would be something like Pair

I'm not a C# programmer, so I won't give you exact code.

You have two options.

First one, more C-ish: out parameters:

void test(out bool p1, out bool p2) {
  p1 = true;
  p2 = false;
}

Second one, more OO, use a struct or class to contain your results:

class Result {
   public bool Part1 {get;set;}
   public bool Part2 {get;set;}
}

Result test() {
   return new Result {Part1 = true, Part2 = false};
}

In C# 4 you can use the Tuple class instead of creating your own result class.

I did not look at your specific requirement but you can combine the methods if you need, but keep in mind that the out parameters are discouraged in object oriented programming at least as per microsoft guidelines.

You might want to use out -parameters.

protected bool IsFilePresent(out string fileContents) {
  string filePath = Server.MapPath("/myFile.txt");
  try {
    fileContents = File.ReadAllText(filePath);
    return true;
  } catch {
    fileContents = null;
    return false;
  }
}

Yes you can use output parameters.

public bool GetData(string filename, out string data)
{
    if (File.Exists(filename))
    {
        data = File.ReadAllText(filename);
        return true;
    }
    data = string.Empty;
    return false;
}

string data;
if (GetData(Server.MapPath("/myFile.txt"), out data))
{
     // do something with data
}

Yes, we can. dotNet 4 support dynamic type, so you can return more than one type

    public dynamic GetSomething()
    {
        if (!File.Exists(Server.MapPath("/myFile.txt")))
            return false;
        else
            return File.ReadAllText(Server.MapPath("/myFile.txt"));
    }

 var result = GetSomething();
 if(result is bool && result == false)
 {
     //doSomething();
 }

This way is hard to read your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM