You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since ref struct is stack-only, it could have a destructor, which would be executed when the struct goes out of scope. If I write a function library for others, although I provide dispose function, users can still ignore it and forget to release resources. I can't force them to use "using". But if ref struct supports destructor, I can use it to wrap my functions to ensure releasing resources.
#7029
Closed
SamiG42 opened this issue
Mar 4, 2023
· 0 comments
Since ref struct is stack-only, it could have a destructor, which would be executed when the struct goes out of scope. If I write a function library for others, although I provide dispose function, users can still ignore it and forget to release resources. I can't force them to use "using". But if ref struct supports destructor, I can use it to wrap my functions to ensure releasing resources.
For example:
refstructA{FileStreamfile;publicA(stringfilename){file=newFileStream(filename,FileMode.Open);}
...... //other member functions~A()//destructor {if(file){file.Close();file=null;}}}staticvoidtest(){Aa=newA("D:\test.txt");
......
}//destructor of "a" would be executed here automatically
For example:
related issue: #231
Add a rule: ref struct with destructor can't assign each other.
Originally posted by @ygc369 in #1110
The text was updated successfully, but these errors were encountered: