Q26. What is the difference between Dispose and Finalize keyword in C#?
Q27. What are the main components of CLR in C#?
Q28. Difference between CTS and CLS?.
Q29. How to implement IDispose in C#? give rough code.
Q30. How to implement Finalize in C#? give rough code.
---------------------------------------------------------------------------------------------------------------------------------
Q26. What is the difference between Dispose and Finalize keyword in C#?
Answer:
When the .NET framework instantiates an object, it allocates memory for that object on the managed heap. The object remains on the heap until it's no longer referenced by any active code and then it will be garbage collected by GC.
.NET Framework provides two methods Finalize and Dispose for releasing unmanaged resources like files, database connections, COM etc. This article helps you to understand the difference between Finalize and Dispose method.
A Dispose method should call the GC.SuppressFinalize() method for the object of a class which has destructor because it has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method.
Dispose
Finalize
It is used to free unmanaged resources like files, database connections etc. at any time.
It can be used to free unmanaged resources (when you implement it) like files, database connections etc. held by an object before that object is destroyed.
Explicitly, it is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface.
Internally, it is called by Garbage Collector and cannot be called by user code.
Although we can do is call destructor.
Although we can do is call destructor.
It belongs to IDisposable interface.
It belongs to Object class.
It's implemented by implementing IDisposable interface Dispose() method.
It's implemented with the help of destructor in C++ & C#.
There is no performance costs associated with Dispose method.
There is performance costs associated with Finalize method since it doesn't clean the memory immediately and called by GC automatically.
---------------------------------------------------------------------------------------------------------------------------------
Q27. What are the main components of CLR in C#?
Answer:
CLR - Common Language Runtime has below 4 main components
- CLS - Common language Specification
- CTS - Common Type specification
- GC - Garbage collector
- JIT - Just in Time compiler.
---------------------------------------------------------------------------------------------------------------------------------
Q28. Difference between CTS and CLS?
Answer:
Both are used for cross language communication and type safety.
CTS:
It defines rules that every language must follow which runs under .NET framework. It ensures that objects written in different .NET languages like C#, VB.NET, F# etc. can interact with each other. This is more about type safety.
CLS:
It defines a set of rules and restrictions that every language must follow which runs under .NET framework. This is more about communication.
For example, one rule is that you cannot use multiple inheritance within .NET Framework. As you know C++ supports multiple inheritance but; when you will try to use that C++ code within C#, it is not possible because C# doesn’t supports multiple inheritance.
---------------------------------------------------------------------------------------------------------------------------------
Q29. How to implement IDispose in C#? give rough code.
Answer:
Implementing Idisposable
public class MyClass : IDisposable
{
private bool disposed = false;
//Implement IDisposable.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// TO DO: clean up managed objects
}
// TO DO: clean up unmanaged objects
disposed = true;
}
}
}--------------------------------------------------------------------------------------------------------------------------------
Q30. How to implement Finalize in C#? give rough code.
Answer:
Finalize keyword usage// Using Dispose and Finalize method together
public class MyClass : IDisposable
{
private bool disposed = false;
//Implement IDisposable.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// TO DO: clean up managed objects
}
// TO DO: clean up unmanaged objects
disposed = true;
}
}
//At runtime C# destructor is automatically Converted to Finalize method
~MyClass()
{
Dispose(false);
}
}
No comments:
Post a Comment