C# Interview Questions on Destructors
What is a Destructor?
A Destructor has the same name as the class
with a tilde character and is used to destroy an instance of a class.
Can a class have more than 1 destructor?
No, a class can have only 1 destructor.
Can structs in C# have destructors?
No, structs can have constructors but not
destructors, only classes can have destructors.
Can you pass parameters to destructors?
No, you cannot pass parameters to
destructors. Hence, you cannot overload destructors.
Can you explicitly call a destructor?
No, you cannot explicitly call a
destructor. Destructors are invoked automatically by the garbage collector.
Why is it not a good idea to use Empty
destructors?
When a class contains a destructor, an
entry is created in the Finalize queue. When the destructor is called, the
garbage collector is invoked to process the queue. If the destructor is empty,
this just causes a needless loss of performance.
Is it possible to force garbage collector
to run?
Yes, it possible to force garbage collector
to run by calling the Collect() method, but this is not considered a good
practice because this might create a performance over head. Usually the programmer
has no control over when the garbage collector runs. The garbage collector
checks for objects that are no longer being used by the application. If it
considers an object eligible for destruction, it calls the destructor(if there
is one) and reclaims the memory used to store the object.
Usually in .NET, the CLR takes care of
memory management. Is there any need for a programmer to explicitly release
memory and resources? If yes, why and how?
If the application is using expensive
external resource, it is recommend to explicitly release the resource before
the garbage collector runs and frees the object. We can do this by implementing
the Dispose method from the IDisposable interface that performs the necessary
cleanup for the object. This can considerably improve the performance of the
application.
When do we generally use destructors to
release resources?
If the application uses unmanaged resources
such as windows, files, and network connections, we use destructors to release
resources.
----------------
C# Interview Questions on constructors
What is a constructor in C#?
Constructor is a class method that is
executed when an object of a class is created. Constructor has the same name as
the class, and usually used to initialize the data members of the new object.
In C#, What will happen if you do not
explicitly provide a constructor for a class?
If you do not provide a constructor
explicitly for your class, C# will create one by default that instantiates the
object and sets all the member variables to their default values.
Structs are not reference types. Can
structs have constructors?
Yes, even though Structs are not reference
types, structs can have constructors.
We cannot create instances of static
classes. Can we have constructors for static classes?
Yes, static classes can also have
constructors.
Can you prevent a class from being
instantiated?
Yes, a class can be prevented from being
instantiated by using a private constructor as shown in the example below.
using System;
namespace TestConsole
{
class Program
{
public static void Main()
{
//Error cannot create instance of a class with private constructor
SampleClass SC = new SampleClass();
}
}
class SampleClass
{
double PI = 3.141;
private SampleClass()
{
}
}
}
Can a class or a struct have multiple
constructors?
Yes, a class or a struct can have multiple
constructors. Constructors in csharp can be overloaded.
Can a child class call the constructor of a
base class?
Yes, a child class can call the constructor
of a base class by using the base keyword as shown in the example below.
using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string str)
{
Console.WriteLine(str);
}
}
class ChildClass : BaseClass
{
public ChildClass(string str): base(str)
{
}
public static void Main()
{
ChildClass CC = new ChildClass("Calling base class constructor from
child class");
}
}
}
If a child class instance is created, which
class constructor is called first - base class or child class?
When an instance of a child class is
created, the base class constructor is called before the child class
constructor. An example is shown below.
using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass()
{
Console.WriteLine("I am a base class constructor");
}
}
class ChildClass : BaseClass
{
public ChildClass()
{
Console.WriteLine("I am a child class constructor");
}
public static void Main()
{
ChildClass CC = new ChildClass();
}
}
}
Will the following code compile?
using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string str)
{
Console.WriteLine(str);
}
}
class ChildClass : BaseClass
{
public ChildClass()
{
Console.WriteLine("I am a child class constructor");
}
public static void Main()
{
ChildClass CC = new ChildClass();
}
}
}
No, the above code will not compile. This
is because, if a base class does not offer a default constructor, the derived class
must make an explicit call to a base class constructor by using the base
keyword as shown in the example below.
using System;
namespace TestConsole
{
class BaseClass
{
public BaseClass(string str)
{
Console.WriteLine(str);
}
}
class ChildClass : BaseClass
{
//Call the base class contructor from child class
public ChildClass() : base("A call to base class constructor")
{
Console.WriteLine("I am a child class constructor");
}
public static void Main()
{
ChildClass CC = new ChildClass();
}
}
}
Can a class have static constructor?
Yes, a class can have static constructor.
Static constructors are called automatically, immediately before any static
fields are accessed, and are generally used to initialize static class members.
It is called automatically before the first instance is created or any static
members are referenced. Static constructors are called before instance
constructors. An example is shown below.
using System;
namespace TestConsole
{
class Program
{
static int I;
static Program()
{
I = 100;
Console.WriteLine("Static Constructor called");
}
public Program()
{
Console.WriteLine("Instance Constructor called");
}
public static void Main()
{
Program P = new Program();
}
}
}
Can you mark static constructor with access
modifiers?
No, we cannot use access modifiers on
static constructor.
Can you have parameters for static
constructors?
No, static constructors cannot have
parameters.
What happens if a static constructor throws
an exception?
If a static constructor throws an
exception, the runtime will not invoke it a second time, and the type will
remain uninitialized for the lifetime of the application domain in which your
program is running.
Give 2 scenarios where static constructors
can be used?
1. A typical use of static constructors is
when the class is using a log file and the constructor is used to write entries
to this file.
2. Static constructors are also useful when
creating wrapper classes for unmanaged code, when the constructor can call the
LoadLibrary method.
Does C# provide copy constructor?
No, C# does not provide copy constructor.
No comments:
Post a Comment