© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
9 replies
Keeda

❔ When does the IDisposable objects gets automatically disposed ?

public bool Method()
{
  bool result = false;
  using(var disposableClass = new DisposableClass())
  {
    result = disposableClass.SomeMethod();
    //1. return result; 
  }
  return result; //2.
}
public bool Method()
{
  bool result = false;
  using(var disposableClass = new DisposableClass())
  {
    result = disposableClass.SomeMethod();
    //1. return result; 
  }
  return result; //2.
}

Now my question is in which case the memory will be disposed if I return it inside the using case or outside of the using case or if it does not matters where the result is returned?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Destroy objects in CSharp, IDisposable
C#CC# / help
4y ago
IDisposable Constructor Injection
C#CC# / help
12mo ago
IDisposable ownership semantics
C#CC# / help
15mo ago
❔ Service provider, IDisposable
C#CC# / help
3y ago