Showing posts with label FrequentlyAsked. Show all posts
Showing posts with label FrequentlyAsked. Show all posts

Wednesday, 4 August 2021

What is the differnece between Abstract class & Interface?

In an abstract class, we can create the functionality and that needs to be implemented by the derived class. The interface allows us to define the functionality or functions but cannot implement that. The derived class extends the interface and implements those functions.

Abstract ClassInterface
Abstract classes are inherited.Interfaces are Implemented.
It is a half-defined parent class.It is a contract.
Share some common logic in child classesPlanning Abstraction.
It contains both declaration and definition parts.It contains only a declaration part.
It contains both declaration and definition parts.It contains only a declaration part.
Multiple inheritance is not achieved by abstract class.Multiple inheritance is achieved by interface.
It contain constructor.It does not contain constructor.
It can contain static members.It does not contain static members.
It can contain different types of access modifiers like public, private, protected etc.It only contains public access modifier because everything in the interface is public.
The performance of an abstract class is fast.The performance of interface is slow because it requires time to search actual method in the corresponding class.
It is used to implement the core identity of class.It is used to implement peripheral abilities of class.
A class can only use one abstract class.A class can use multiple interface.
If many implementations are of the same kind and use common behavior, then it is superior to use abstract class.If many implementations only share methods, then it is superior to use Interface.
Abstract class can contain methods, fields, constants, etc.Interface can only contain methods .
It can be fully, partially or not implemented.
It should be fully implemented.
Sr. No.KeyAbstract ClassInterface
1DefinitionIn terms of standard definition, an Abstract class is, conceptually, a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions.On other hand an Interface is a description of what member functions must a class, which inherits this interface, implement. In other words, an interface describes behaviour of the class.
2ImplementationAs like of other general class design in C# Abstract class also have its own implementation along with its declaration.On other hand an Interface can only have a signature, not the implementation. While its implementation is being provided by the class which implements it.
3InheritanceAs per specification in C# a class can extends only one other class hence multiple inheritance is not achieved by abstract class.On other hand in case of Interface a class can implements multiple interfaces and hence multiple inheritance is achieved by interface.
4ConstructorLike other classes in C# for instantiation abstract class also have constructor which provide an instance of abstract class to access its non-static methods.On other hand Interface do not have constructor so we can't instantiate an interface directly although its method could get accessed by creating instance of class which implementing it.
5ModifiersAs abstract class is most like of other ordinary class in C# so it can contain different types of access modifiers like public, private, protected etc.On other hand as Interface needs to be get implemented in order to provide its methods implementation by other class so can only contains public access modifier.
6PerformanceAs abstract class have its method as well as their implementations also for its abstract methods implementation it have reference for its implementing class so performance is comparatively faster as compare to that of Interface.On other hand the performance of interface is slow because it requires time to search actual method in the corresponding class.
Watch video to clear the concept-

Friday, 23 May 2014

What is the use/significance of Dispose and Finalize method?

.NET Framework provides two methods Finalize and Dispose for releasing unmanaged resources like: Windows API created objects, File, Database connection objects, COM objects.
It is always recommended to use Dispose method to clean unmanaged resources. Do not implement the Finalize method until it is extremely necessary
Dispose:

Dispose method belongs to ‘IDisposable’ interface. If any object wants to release its unmanaged code, the best is to implement IDisposable and override the Dispose method  of IDisposable 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.

 public class CleanClass : IDisposable
    {
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
        
        ~CleanClass()
        {
            Dispose();
         }
    }

Finalize:

.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (example: Windows API created objects, File, Database connection objects, COM objects, etc.) are outside the scope of .NET Framework. We have to explicitly clean our resources. For these types of objects, .NET Framework provides
Object.Finalize method.


What is the difference between Finalize() and Dispose() methods?

Dispose:
  1. Dispose It belongs to IDisposable interface. and internal called by user code.
  2. Dispose () of IDisposable interface is called by the programmer to explicitly release resources when they are no longer being used. Dispose () can be called even if other references to the object are alive.
  3. It is called by user code and the class implementing dispose method must implement IDisposable interface.It belongs to IDisposable interface.Implement this when you are writing a custom class that will be used by other users.There is no performance costs associated with Dispose method
Finalize:
  1. Finalize It belongs to Object class. and It's implemented with the help of destructor in C#.
  2. Used to free unmanaged resources like files, database connections, COM etc. held by an object before that object is destroyed.Internally, it is called by Garbage Collector and cannot be called by user code.
  3. The finalizer method is called when your object is garbage collected and you have no guarantee when this will happen (you can force it, but it will hurt performance).

Why is it preferred to not use finalize for clean up?

The problem with f
inalize is that garbage collection has to make two rounds in order to remove objects which have finalize methods.

Let us assume, there are three objects, Object1Object2, 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. It 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. It sees 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, 
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/unmanaged resources in Finalize method rather use the DISPOSE.

What is the purpose of the Using block in C#?

The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.

public class Debuggersspace : IDisposable
{
 //implementation details...
}



These are equivalent:
Debuggersspace qa = new Debuggersspace ();
try 
{
        qa.Action();
}
finally
{
       if (qa != null)
       qa.Dispose();
}

In other words, the using statement tells .NET to release the object specified in the using block once it is no longer needed

using (Debuggersspace qa = new Debuggersspace ())
{
    qa.Action(); 
}
Points to remember:
  1. If you declare the variable outside the using block and then create a new instance in the using statement it may not dispose the item
  2. So the using statement will automatically dispose of the object once that context is complete.
  3. The using statement is used to work with an object in C# that implements the IDisposableinterface.
  4. The IDisposable interface has one public method called Dispose that is used to dispose of the object.
  5. we use the using statement, we don't need to explicitly dispose of the object in the code, the using statement takes care of it.


C# Disposable pattern, Using, Dispose Vs Finalize:




Please refer below links for more information:


Wednesday, 7 May 2014

ASP.NET Page Life Cycle Events?

PreInit:

  • Check for the IsPostBack property to determine whether this is the first time the page is being processed.
  • Create or recreate dynamic controls.
  • Set master page dynamically.
  • Set the Theme property dynamically.
  • Read or set profile property values.
If Request is postback:
  • The values of the controls have not yet been restored from view state.
  • If you set control property at this stage, its value might be overwritten in the next event.

Init:

  • In the Init event of the individual controls occurs first, later the Init event of the Page takes place.
  • This event is used to initialize control properties.

InitComplete:

  • Tracking of the ViewState is turned on in this event.
  • Any changes made to the ViewState in this event are persisted even after the next postback.

PreLoad:

  • This event processes the postback data that is included with the request.

Load:

  • In this event the Page object calls the OnLoad method on the Page object itself, later the OnLoad method of the controls is called.
  • Thus Load event of the individual controls occurs after the Load event of the page.

ControlEvents:

  • This event is used to handle specific control events such as a Button control’s Click event or a TextBoxcontrol’s TextChanged event.
In case of postback:
  • If the page contains validator controls, the Page.IsValid property and the validation of the controls takes place before the firing of individual control events.

LoadComplete:

  • This event occurs after the event handling stage.
  • This event is used for tasks such as loading all other controls on the page.

PreRender:

  • In this event the PreRender event of the page is called first and later for the child control.
Usage:
  • This method is used to make final changes to the controls on the page like assigning the DataSourceId and calling the DataBind method.

PreRenderComplete:

  • This event is raised after each control's PreRender property is completed.

SaveStateComplete:

  • This is raised after the control state and view state have been saved for the page and for all controls.

RenderComplete:

  • The page object calls this method on each control which is present on the page.
  • This method writes the control’s markup to send it to the browser.

Unload:

  • This event is raised for each control and then for the Page object.
Usage:
  • Use this event in controls for final cleanup work, such as closing open database connections, closing open files, etc.

How to improve applications performance which is hosted in cloud ?

Improving the performance of an application hosted in Microsoft Azure involves a combination of optimizing your application code, leveraging...