Saturday, 28 August 2021

Constructor

1. What is a Constructor in C#?

A constructor is a special method that runs when an object of a class is created. It initializes the object and allocates memory. The constructor has the same name as the class and does not have a return type.

Key Points:

  • Same name as the class.
  • No return type, not even void.
  • Can have parameters.
  • Can have access modifiers.
  • Cannot return a value, but can use return;.

2. Can we define a method with the same name as the class in C#?

No, you cannot have a method with the same name as the class. It will result in a compile-time error.


3. How many types of constructors are there in C#?

  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor
  • Static Constructor
  • Private Constructor

4. What is a Default Constructor?

A default constructor has no parameters. It can be:

  • System-defined: Automatically provided by C#.
  • User-defined: Explicitly created by the developer.

5. When should we define a Parameterized Constructor?

Use a parameterized constructor when you want to initialize an object with specific values provided by the user.


6. What is a Parameterized Constructor?

A parameterized constructor allows you to set different values for each instance of a class. This way, you can create objects with distinct initial values.


7. How many constructors can be defined in a class?

You can define multiple constructors in a class as long as they have different parameter types or orders. This includes one default constructor and any number of parameterized constructors.


8. What is a Copy Constructor in C#?

A copy constructor takes an object of the same class as a parameter and initializes a new object with the values from that existing object.


9. Explain the Static Constructor in C#.

A static constructor is called once, regardless of how many instances of the class are created. It's used to initialize static fields and cannot have parameters or access modifiers.


10. What is a Private Constructor in C#?

A private constructor restricts object creation from outside the class. It's often used in design patterns like Singleton, ensuring only one instance of the class exists.


11. When is a Destructor called?

A destructor is called automatically when an object is destroyed, usually by the garbage collector.


12. When does an object get destroyed?

An object can be destroyed when:

  1. The program ends.
  2. The garbage collector runs due to memory pressure.
  3. You explicitly call GC.Collect().

13. Can a child class call the constructor of a base class?

Yes, a child class can call a base class constructor using the base keyword.


14. Which constructor is called first when a child class instance is created?

The base class constructor is called first, followed by the child class constructor.


15. Can you mark a static constructor with access modifiers?

No, static constructors cannot have access modifiers.


16. Can static constructors have parameters?

No, static constructors cannot accept parameters.


17. Give two scenarios where static constructors can be useful.

  • Initializing a log file for a class.
  • Loading unmanaged code with LoadLibrary.

18. What is a Destructor?

A destructor is a method that is called when an instance of a class is destroyed. It has the same name as the class, prefixed with a tilde (~).


19. Can a class have more than one destructor?

No, a class can only have one destructor.


20. Can you pass parameters to destructors?

No, destructors cannot take parameters and cannot be overloaded.


21. Can you explicitly call a destructor?

No, destructors are automatically invoked by the garbage collector.


22. When should you use a Private Constructor?

  • To prevent instantiation from outside the class.
  • To implement the Singleton design pattern.
  • When some constructors should only be used internally.

23. If a class has a parameterized constructor, do you need a default constructor?

If you have a parameterized constructor, you need to define a default constructor explicitly if you want one.


24. Can constructors be inherited?

No, constructors cannot be inherited.


25. How can you call a base class constructor from a child class?

You can call a base class constructor using the base keyword in the child class constructor.


26. In which order are constructors called in inheritance?

When creating an object of a derived class, the base class constructor is called first, followed by the derived class constructor.


27. Can constructors be overloaded?

Yes, constructors can be overloaded by varying parameters.


28. How do you overload a constructor?

You overload a constructor by defining it with different parameter types or numbers.


29. How do you call one constructor from another in the same class?

You can call one constructor from another using the this keyword.


30. Which constructor is called first when you create an object of a class?

The first overloaded constructor is called, followed by the default constructor if it exists.


31. Can a constructor call itself using the this keyword?

No, doing so will result in a compile-time error.


32. Does memory get allocated when a constructor is called?

Yes, memory is allocated using the new operator when a constructor is invoked.

No comments:

Post a Comment

Constructor

1. What is a Constructor in C#? A constructor is a special method that runs when an object of a class is created. It initializes the object ...