Saturday, December 22, 2018

What is Manage and Unmanage Code

Manage Code:

Managed code is a code which is executed by CLR (Common Language Runtime) i.e all application code based on .Net Platform. It is considered as managed because of the .Net framework which internally uses the garbage collector to clear up the unused memory.

Unmanaged Code:

Unmanaged code is any code that is executed by application runtime of any other framework apart from .Net. The application runtime will take care of memory, security and other performance operations.


Sunday, December 16, 2018

Difference between Finalize and Dispose?

Ans:

The main difference between dispose() and finalize() is that the method dispose() has to be explicitly invoked by the user whereas, the method finalize() is invoked by the garbage collector, just before the object is destroyed.


BASIS FOR COMPARISONDISPOSE( )FINALIZE( )
DefinedThe method dispose( ) is defined in the interface IDisposable interface.The method finalize( ) id defined in java.lang.object class.
Syntaxpublic void Dispose( ){
// Dispose code here
}
protected void finalize( ){
// finalization code here
}
InvokedThe method dispose( ) is invoked by the user.The method finalize( ) is invoked by the garbage collector.
PurposeMethod dispose( ) is used to free unmanaged resources whenever it is invoked.Method finalize( ) is used to free unmanaged resources before the object is destroyed.
ImplementationThe method dispose( ) is to be implemented whenever there is a close( ) method.The method finalize( ) is to be implemented for unmanaged resources.
Access specifierThe method dispose( ) is declared as public.The method finalize( ) is declared as private.
ActionThe method dispose( ) is faster and instantly disposes an object.The method finalize is slower as compared to dispose
PerformanceThe method disposes( ) performs the instantaneous action hence, does not effect the performance of websites.The method finalize( ) being slower affects the performance of the websites.

What is Using & Why we use Using?

Ans:

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.


The using statement tells .NET to release the object specified in the using block once it is no longer needed.


using (B a = new B())
{
   DoSomethingWith(a);
}
is equivalent to
B a = new B();
try
{
  DoSomethingWith(a);
}
finally
{
   ((IDisposable)a).Dispose();
}
if we declare class object in under using state it got compellation error convert to Idispoable.

Tuesday, December 4, 2018

What is a nested class? And when should you use nested classes in your C# programs?

The class B here is enclosed inside the declaration of class A. Class B is thus a nested class. Because it has a public accessibility modifier, it can be accessed in places other than class A's scope.

We create an instance of A and an instance of A.B. The instance of A does not contain an instance of B.

Example:

class A
{
    public int _v1;

    public class B============nested classs
    {
        public int _v2;
    }
}

class Program
{
    static void Main()
    {
        A a = new A();
        a._v1++;

        A.B ab = new A.B();
        ab._v2++;
    }
}

Note: The main feature of nested classes is that they can access private members of the outer class while having the full power of a class itself. Also they can be private which allows for some pretty powerful encapsulation in certain circumstances:\

  • A nested class can be declared as a private, public, protected, internal, protected internal, or private protected.
  • Outer class is not allowed to access inner class members directly as shown in above example.
  • You are allowed to create objects of inner class in outer class.
  • Inner class can access static member declared in outer class as shown in the below example:
  • Inner class can access non-static member declared in outer class as shown in the below example: