How to return from the function response return different types?

Has written for the convenience of code to open file in a new function.
I now realize the answer from the options below:
1. with a successful opening to the List
2. if you fail to get a bool parameter or string variable with the text of the error

private void btn_StartRegMail_Click(object sender, EventArgs e)
{
 var names = new List<string>();
 //Open a file with the names
 names = OpenFile(tB_names.Text);
}
// Open file
private static List<string> OpenFile(string file)
{
 if (File.Exists(file) == true)
{
 var list = new list<string>();
 FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
 using (StreamReader sr = new StreamReader(fs, Encoding.Default))
{
 string temp = string.Empty;
list.Add(temp);
}
fs.Close();
 return list;
}
else
{
 MessageBox.Show("Specified file not found!");
 //return "error";
}
}</string></string></string>
July 9th 19 at 11:06
4 answers
July 9th 19 at 11:08
Solution
Don't do that, but if you want:
private static object OpenFile(string file)
{
...
}

And then
var res = OpenFile(file);
if (res.GetType() == typeof(string))
{
 var error = (string)res;
}


UPD:
How to do:
1. Define your class, for example
class ErrorStatus
{
 public bool Status { get; private set; }
 public string ErrorMessage { get; private set; }

 public ErrorStatus(bool status, string errormessage)
{
 Status = status;
 ErrorMessage = errormessage;
}
}

2. And bring already
private static ErrorStatus OpenFile(string file, out List<string> data)
{
...
}</string>
and how is done? - mohammed.Ritch commented on July 9th 19 at 11:11
: Made addition - candida_Upton commented on July 9th 19 at 11:14
July 9th 19 at 11:10
If you generate a List, while not a critical error (file empty) should return empty List. If the error is critical (file not found, unable to open the file for reading, invalid argument null argument), then you need to throw an exception.

private static List<sometype> ReadFromFile(string path) {
 if (path == null) {
 throw new ArgumentNullException("path");
}
 if (File.Exists(path)) {
 string file = File.ReadAllText(path);
...
 } else {
 throw new FileNotFoundException(path);
}
}</sometype>


Then in the calling code you can do the check:

private void doTheBestBtn_Click(object sender, EventArgs e)
{
 try {
 var myList = ReadFromFile(txtInput.Text);
...
}
 catch(ArgumentNullException ex) {
 MessageBox.Show("you Must specify a file path!");
}
 catch(FileNotFoundException ex) {
 MessageBox.Show("Specified file not found!");
}
}


PS File.ReadAllText he throws some exceptions, but assume that it is self-made type.
July 9th 19 at 11:12
Correct, you need to create Exception if something goes wrong and the handler to provide an alternative logic
Read more
July 9th 19 at 11:14
On error, throw an exception.
you can read more? - mohammed.Ritch commented on July 9th 19 at 11:17
: Google try-catch and exception handling (exceptions). I'm With# unfamiliar, so this can not lead. - candida_Upton commented on July 9th 19 at 11:20

Find more questions by tags C#