Search This Blog

Q26-Q30

Q26. What are the two different types of memory in C#?
Q27. How we can call Garbage collector explicitly?
Q28. Write a linq query to use AsQuerable while getting multiple records? Also how to use AsQuerable in case of joins?
Q29. What are the advantages of Tasks over Thread? Why Tasks came in picture when we  have thread in C#?
Q30. Difference between String and StringBuilder class?
------------------------------------------------------------------------------------------------------------------------------------------
Q26. What are the two different types of memory in C#?

Answer:
Stack and Heap. 

GC works only on Heap. GC is not required on Stack Memory because this get auto deallocated once the variable goes out of scope. 

Value types of variables like int, bool, float, short, enum, struct are part of Stack memory. 

Reference types like string, objects, classes are part of heap memory


------------------------------------------------------------------------------------------------------------------------------------------
Q27. How can we call Garbage collector explicitly?

Answer:
Garbage collector can be called explicitly with GC.Collect()
GC.Collect(0): Will clear zero generation objects
GC.Collect(1): Will clear zero and 1st generation objects.
GC.Collect(2): Will clear all generation objects (Zero, 1st, 2nd)

Although we don't need this as Garbage collector take care of collecting in destructor only by calling GC.Finalize. GC.Finalize cannot be called explicitly.

for managing unmanaged code, we use Dispose method. 


------------------------------------------------------------------------------------------------------------------------------------------
Q28. Write a linq query to use AsQuerable while getting multiple records? Also how to use AsQuerable in case of joins?

Answer:




------------------------------------------------------------------------------------------------------------------------------------------
Q29. What are the advantages of Tasks over Thread? Why Tasks came in picture when we have thread in C#?

Answer:
Task is a newer concept than Thread. 

  1. Task can return a result. There is no direct mechanism to return the result from thread.
  2. We can chain tasks together to execute one after the other.
  3. Establish a parent/child relationship when one task is started from another task.
  4. Task support cancellation through the use of cancellation tokens.
  5. Asynchronous implementation is easy in task, using’ async’ and ‘await’ keywords.

------------------------------------------------------------------------------------------------------------------------------------------
Q30. Difference between String and StringBuilder class?

Answer:
Stringbuilder is mutable while string not mutable. Mutable means when we try to change the value of original object it will adopt those changes.

When we make changes in value of a string variable it create a new instance of it as it is immutable and thus it become a costly process from memory point of view but stringbuilder doesn't create any new instance. 

Append is the method used in stringbuilder. 

StringBuilder sbMyValue = new StringBuilder("");
 sbMyValue.Append("Hello Visitor");
 sbMyValue.Append("How Are You ??");
------------------------------------------------------------------------------------------------------------------------------------------

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. 

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