C# (Sharp) Programming Language Question:

Download Job Interview Questions and Answers PDF

How do I get deterministic finalization in C#?

C# (Sharp) Programming Language Interview Question
C# (Sharp) Programming Language Interview Question

Answer:

In a garbage collected environment, it'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:

using(FileStream myFile = File.Open(@"c:temptest.txt",
FileMode.Open))
{
int fileOffset = 0;
while(fileOffset < myFile.Length)
{
Console.Write((char)myFile.ReadByte());
fileOffset++;
}
}
When myFile leaves the lexical scope of
the using, its dispose method will be called.

Download C# (Sharp) Programming Language Interview Questions And Answers PDF

Previous QuestionNext Question
What does assert() do in C#?How can I get around scope problems in a try/catch?