2
Answers

Is IDisposable interface just used for standardization ?

Kg P

Kg P

7y
344
1
Hi, I might be thinking wrong, please correct if I am wrong.
To cleanup memory (Specially for unmanaged resources), we implement IDisposable interface and impelement Dispose method in which we write our custom logic to free memory.
So, my question is that as we are writing our own logic to free memory, can't we do same in any public method of our cass ?
Means actually we don't need IDisposable and Dispose method for this ? May be we use these for this kind of reasons-
1. Just for standardization that they are same across all applications
2. Once we say class implements IDisposable interface, we make sure that we don't miss dispose method implementation as we'll get error
if we forgot to give implementation for dispose method.
OR there is some other reason for using IDisposable. Please share your thoughts.
For example-
public class MyClass{
public void ReleaseMemory(){
ReleaseMemory(true);
GC.SuppressFinalize();
}
public void ReleaseMemory(bool releasing){
//all memory cleanup logic
}
}
Does this solve the purpose without implementing IDisposable interface ??
Answers (2)
1
Mehul Prajapati

Mehul Prajapati

NA 65 1.5k 7y
chrome.runtime.sendMessage have limited size to parameter. os i have used URL.createObjectURL to transfer data from content to background js.
 
Content.js 
 
var filedata = JSON.stringify({ data: data, fileName: fileName });
var save = new File([filedata], "filename.json", { type: "text/json;charset=utf-8" });
var postdata = window.URL.createObjectURL(save);
 
Background.js
 
var xhr = new XMLHttpRequest();
xhr.open('GET', postdata, true);
xhr.responseType = "text";
xhr.onload = function (e) {
   if (this.status == 200) {
      var myBlob = this.response;
      // myBlob is now the blob that the object URL pointed to.
   }
};
xhr.send();