Monday, March 30, 2015

How do I get deterministic finalization in C#?

Question:How do I get deterministic finalization in C#??

Answer: </P><P align=justify>In a garbage collected environment, it&#39;s impossible to get true determinism. However, a design pattern that we recommend is implementing IDisposable on any class that contains a critical resource. Whenever this class is consumed, it may be placed in a using statement, as shown in the following example: </P><P align=justify>using(FileStream myFile = File.Open(@"c:temptest.txt", </P><P align=justify>FileMode.Open))</P><P align=justify>{</P><P align=justify>int fileOffset = 0;</P><P align=justify></P><P align=justify>while(fileOffset &lt; myFile.Length)</P><P align=justify>{</P><P align=justify>Console.Write((char)myFile.ReadByte());</P><P align=justify>fileOffset++;</P><P align=justify>}</P><P align=justify>} </P><P align=justify></P><P align=justify>When myFile leaves the lexical scope of the using, its dispose method will be called.

Tags:Interview Questions, Microsoft, C#,

Please submit your Interview Answer for the above Question via replying to this post. Thank you for visiting to our blog, Have a Good Day!.



Sponsor Advertisement



Human Resource Management Software

Human Resource Management Software

Powerful, configurable software for human resources management that is easy to manage and remarkably cost-effective, whatever the size of your company. Now there’s a smarter, more efficient way to handle all of your people-related activities. Easy-to-use system manages the full hire-to-retire lifecycle...

No comments:

Post a Comment