I am looking at a problem where a method can fail in a predictable way. For now I think that exceptions might not be a good way to represent some of the failures (I may be wrong on that). In any case I was thinking of returning a Result type similar to the ones in ASP.NET Identity and the ones discussed in this question - What is a good design for a method that can return several logically different results? (i.e. a bool or enum property Result and other properties which are present if the result is success). However since C# 7 is now released and it has pattern matching I wonder if this is not a better option

public abstract class ReadFileResult { } public class FileNotFoundResult : ReadFileResult { } public class BytesFileResult : ReadFileResult { public BytesFileResult(byte[] fileData) { FileData = fileData; } public byte[] FileData { get; } } class Program { static void Main(string[] args) { ReadFileResult readFileResult = ReadFile("test.txt"); switch (readFileResult) { case FileNotFoundResult fileNotFound: { Console.WriteLine("File not found"); } break; case BytesFileResult bytesFileResult: { Console.WriteLine(bytesFileResult.FileData.Length); //do something with the data } break; default: { throw new Exception("Unexpected file result"); } } } public static ReadFileResult ReadFile(string filename) { throw new NotImplementedException(); //some logic here } }

Would you prefer to see the "standard" result type + properties way or the pattern matching way is clearer? If you prefer one over the other why? Can the pattern matching based solution be improved?

Edit: Just for the record this example is NOT my use case. My specific use case will need at least 3 catch blocks if I go for exceptions (which I still might do). I lean towards returning a result because I prefer if over 3 catch blocks even if I'd probably go for an exception if it was just 1 odd case. In any case my question is meant to compare the two ways to return a result not to argue about exceptions vs results. Lets assume that I am talking about ASP.NET Identity. Would they be better off if instead of this class they had a bunch of derived classes and used pattern matching (assuming that all their clients used C# 7.0)