What is Post Cache substitution?
Post cache substitution is used when we want to cache the whole page but also need some dynamic region inside that cached page. Some examples like QuoteoftheDay, RandomPhotos and AdRotator etc. are examples where we can implement Post Cache Substitution. Post-cache substitution can be achieved by two means:
- Call the new Response.WriteSubstitution method, passing it a reference to the desired substitution method callback.
- Add a <asp:Substitution> control to the page at the desired location, and set its methodName attribute to the name of the callback method.
You can see we have a static function here “GetDateToString()”. We pass the response substitution callback to the “WriteSubstitution” method. So now, when ASP.NET page framework retrieves the cached page, it automatically triggers your callback method to get the dynamic content. It then inserts your content into the cached HTML of the page. Even if your page has not been cached yet (for example, it’s being rendered for the first time), ASP.NET still
calls your callback in the same way to get the dynamic content. So you create a method that generates some dynamic content, and by doing so you guarantee that your method is always called, and it’s content is never cached. Ok the above example was by using “WriteSubstitution” now lets try to see how we can do by using “<asp:substitution>” control. You can get the “<asp:substitution>” control from the editor toolbox.
Below is a sample code that shows how substitution control works. We have ASPX code at the right hand side and class code at the behind code at the left hand side. We need to provide the method name in the “methodname” attribute of the substitution control.
Why do we need methods to be static for Post Cache substitution?
ASP.NET should be able to call this method even when there is not an instance of your page class available. When your page is served from the cache, the page object is not created. Therefore, ASP.NET skips the page life cycle when the page is coming from cache, which means it will not create any control objects or raise any control events. If your dynamic content depends on the values of other controls, you will need to use a different technique, because these control objects will not be available to your callback.
What is Dispose method in .NET?
.NET provides ‘Finalize’ method in which we can clean up our resources. But relying on this is not always good so the best is to implement ‘Idisposable’ interface and implement the ‘Dispose’ method where you can put your clean up routines.
What is the use of “Overrides” and “Overridable” keywords?
Overridable is used in parent class to indicate that a method can be overridden. Overrides is used in the child class to indicate that you are overriding a method.
Where are all .NET Collection classes located?
‘System.Collection’ namespace has all the collection classes available in .NET.
What is ArrayList?
Array is whose size can increase and decrease dynamically. Array list can hold item of different types. As Array list can increase and decrease size dynamically you do not have to use the REDIM keyword. You can access any item in array using the INDEX value of the array position.
What is a HashTable?
Twist: – What is difference between HashTable and ArrayList?
You can access array using INDEX value of array, but how many times you know the real value of index. Hashtable provides way of accessing the index using a user identified KEY value, thus removing the INDEX problem.
What are queues and stacks?
Queue is for first-in, first-out (FIFO) structures. Stack is for last in, first-out (LIFO) structures.
What is ENUM?
It is used to define constants.
What is nested Classes?
Nested classes are classes within classes. In sample below “ClsNested” class has a “Child Nested” class nested inside it.
What is the significance of Finalize method in .NET?
.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (ex: – Windows API created objects, File, Database connection objects, COM objects etc) is outside the scope of .NET framework we have to explicitly clean our resources. For these types of objects, .NET framework provides Object. Finalize method, which can be overridden and clean up code for unmanaged resources can be put in this section?
Why is it preferred to not use finalize for clean up?
Problem with finalize is that garbage collection has to make two rounds in order to remove objects which have finalize methods. Below figure will make things clear regarding the two rounds of garbage collection rounds
performed for the objects having finalized methods. In this scenario there are three objects Object1, Object2, and Object3. Object2 has the finalize method overridden and remaining objects do not have the finalize method overridden. Now when garbage collector runs for the first time it searches for objects whose memory has to
free. He can see three objects but only cleans the memory for Object1 and Object3. Object2 it pushes to the finalization queue. Now garbage collector runs for the second time. He see’s there are no objects to be released and
then checks for the finalization queue and at this moment, it clears object2 from the memory. So if you notice that object2 was released from memory in the second round and not first. That is why the best practice is not to write clean up Non.NET resources in Finalize method rather use the DISPOSE.
How can we suppress a finalize method?
What is the use of DISPOSE method?
Dispose method belongs to ‘IDisposable’ interface. We had seen in the previous section how bad it can be to override the finalize method for writing the cleaning of unmanaged resources. So if any object wants to release its unmanaged code best is to implement I Disposable and override the Dispose method of I Disposable interface. Now once your class has exposed the Dispose method it is the responsibility of the client to call the Dispose method to do the cleanup.
How do I force the Dispose method to be called automatically, as clients can forget to call Dispose method?
Call the Dispose method in Finalize method and in Dispose method suppress the finalize method using GC.SuppressFinalize. Below is the sample code of the pattern. This is the best way we do clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage collector twice.
In what instances you will declare a constructor to be private?
When we create a private constructor, we cannot create object of the class directly from a client. Therefore, you will use private constructors when you do not want instances of the class to be created by any external client. Example UTILITY functions in project will have no instance and be used with out creating instance, as creating instances of the class would be waste of memory.
Can we have different access modifiers on get/set methods of a property ?
No we cannot have different modifiers same property. The access modifier on a property applies to both its get and set accessors.
Can we have static indexer in C#?
Can two catch blocks be executed?
No, once the proper catch section is executed the control goes finally to block. So there will not be any scenarios in which multiple catch blocks will be executed.
What is the difference between System.String and System.StringBuilder classes?
System. String is immutable; System.StringBuilder can have mutable string where a variety of operations can be performed.