Search This Blog

Q21-Q25

Q21. What is Final keyword in C#? ( Not Finally and Finalize)
Q22. What is the difference between Dispose and Finalize in C#?
Q23. What is SuppressFinalize in C#?
Q24. How to compare two dates in C#?
Q25. Tell me why and when we should use abstract class and a Concreate/normal class ?
--------------------------------------------------------------------------------------------------------------------------
Q21. What is Final keyword in C#? ( Not Finally and Finalize)

Answer :
If you are not talking about finally keyword than there is no keyword like Final in C#. This is a keyword of Java. Same work of final keyword is done by Sealed keyword in C#. 

--------------------------------------------------------------------------------------------------------------------------
Q22. What is the difference between Dispose and Finalize in C#?

Answer:
In C#, both Dispose and Finalize are used for resource management,

Dispose
1. It is used to free unmanaged resources like files, database connections etc. at any time.
2. Using block internally implement IDisponse for all methods. If we have object creation in Using block of managed resource than we have to implement Idispose interface for that method first. 
3. It has to be explicitly called by user. 
4. When implementing Dispose, it’s common to follow the dispose pattern, which includes a finalizer call (GC.SuppressFinalize(this)) to prevent the garbage collector from calling Finalize if Dispose has already been called.

public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }


Finalize
1. Finalize is a special method that is automatically called by the garbage collector (GC) to free up unmanaged resources.
2. This method is only called by the GC. 
3. The destructor in C# is automatically translated into Finalize

Refer Learning Topics # Garbage collector
--------------------------------------------------------------------------------------------------------------------------
Q23. What is SuppressFinalize in C#?

Answer:
This method is used when we have implemented IDisposable interface for unmanaged resource. this is to tell GC that resource has been already cleaned. 

 public void Dispose() // Implement IDisposable
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
--------------------------------------------------------------------------------------------------------------------------
Q24. How to compare two dates in C#?

Answer:
int result = DateTime.Compare(d1, d2);
if result > 0 = means d1 > d2
result = 0; means d1 = d2
result < 0; d1 < d2

--------------------------------------------------------------------------------------------------------------------------
Q25. Tell me why and when we should use abstract class and a Concreate/normal class?

Answer:
Object of abstract class cannot be made but object of normal class can be made. So whenever you want that your class will only act as a base class for others plus no one can create an object of that base class create that class as abstract class. 

--------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment