Trying to see if I'm understanding this correctly. Regretfully, I have not had much experience with IDisposable. We're taking a static class and making it non-static, and it's a class that is responsible for making web service calls in our Xamarin app. This is an object that I would definitely not want lying around. So I thought of having this class inherit the IDisposable interface, and I implemented the methods as shown:
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects). } // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. // TODO: set large fields to null. disposedValue = true; } } // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources. //~WebServiceCallThread() //{ // // Do not change this code. Put cleanup code in Dispose(bool disposing) above. // Dispose(false); //} // This code added to correctly implement the disposable pattern. public void Dispose() { // Do not change this code. Put cleanup code in Dispose(bool disposing) above. Dispose(true); // TODO: uncomment the following line if the finalizer is overridden above. //GC.SuppressFinalize(this); } So all this appears to be doing, is when the Dispose method is called, it then calls a virtual method declared above Dispose(bool disposing). With the current code, this does nothing (or so it seems). It just calls the Dispose method, in which that calls an overloaded Dispose method. But nothing is actually "disposing" here.
Upon some research, it SEEMS (which is why I'm asking, I think I am understanding correctly). It says to only call a finalizer and the GC.SuppressFinalize method IF you dispose managed state. I'm not sure what dispose manage state means, but there's really nothing in my class that needs to get disposed. I just have some global int variables but that's it. The meat of this class is a HttpCall, which is already disposing of the objects automatically, as such:
public async void CallUrlHttpClient(string URL, System.Action<string> Handler, System.Action<XamarinMobile.Classes.ErrorInfo> ErrorHandler, int Tries) { var result = string.Empty; didMakeSuccessfulHttpRequest = false; using (var client = new HttpClient()) { ... Code } ... So here's my question: Do I need to make this class inherit IDisposable? I'm wondering if anything under the hood is being done, but it seems as if my application can just utilize the GC to get rid of my object that uses this class. This is how I call it, via a static method in a class:
public static void WebServiceCall(string URL, System.Action<string> Callback) { //using (XamarinMobile.Classes.WebServiceCallThread thread = new Classes.WebServiceCallThread()) //{ XamarinMobile.Classes.WebServiceCallThread thread = new Classes.WebServiceCallThread(); thread.Call(URL, Callback); //} } If I understand GC correctly, once this method is done, the GC will automatically get rid of the object. So do I really need to make the WebServiceCallThread inherit from IDisposable? Again I'm asking if I'm understanding all of this correctly. If not, please correct me and let me know where I'm confused.
Thanks.
IDisposable, also in fields, then no, you don't need to implementIDisposable.