Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle disposable objects? #226

Open
vyeghikyan-st opened this issue Oct 23, 2024 · 1 comment
Open

How to handle disposable objects? #226

vyeghikyan-st opened this issue Oct 23, 2024 · 1 comment

Comments

@vyeghikyan-st
Copy link

Let us have a class

public class MyClass{
  // Some methods, variables and constructors
  
  public Result<Stream> OpenStream() {
   // Implementation
  }

}

The only pattern I can think of for handling the returned object is as follows:

var instance = new MyClass(..);

var streamReasult = instance.OpenStream();

if(streamResult.IsFailed){
  return Result.Fail<...>(...);
}

try{
  // Do my stuff with the stream
}
finally{
  streamResult.Value.Dispose();
}

Is there a better way?

@Feijo
Copy link

Feijo commented Dec 19, 2024

Apart from the fact that you probably should be using using (var stream = streamResult.Value), your implementation looks correct. Why not use an exception if the stream fails to open, though?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants