Tech Point Fundamentals

Sunday, May 29, 2022

C# Interview Questions and Answers - Part 02

C# Interview Questions and Answers - Part 02


Are you preparing for the C# Interview? If yes, then you are at the right place. This is the C# Interview Questions and Answers article series. Here we will see the top 150+ C# Interview Questions with Answers. 

Please visit our YouTube Channel for Interviews and other videos by below link:

Please read the complete Design Patterns Interview Questions and Answers series here.


This is the 2nd part of this C# Interview Questions and Answers article series. Each part will contain 10 C# Interview Questions with Answers. Please read all the C# Interview Questions list here.

I will highly recommend to please read the previous parts over here before reading the current part:

C# Interview Questions and Answers - Part 02

Q011. Can you achieve method overloading by inheritance? How?

Yes, Method Overloading is possible in inheritance as well. A method that is defined in a class can be overloaded under its child class if we overload a method in this process we call it inheritance-based overloading.

In VB.Net when you are overloading a method of the base class in the derived class, then you must use the keyword “Overloads”. But in C# there is no need to use any keyword while overloading a method either in the same class or in the derived class.

When the compiler is looking for an overloaded instance method, it considers the compile-time class of the "target" call and looks at methods declared there. If it can't find anything suitable match, it then looks at the parent class and at last the grandparent class.

So if there are two matching methods at different levels of the hierarchy, the "deeper" one will be chosen first, even if it is not a "better function member" for the call.

Please read the inheritance-based overloading rule here for more details.

Q012. Can you overload the main() method in C#? Overriding the main method is possible or not?

The Main() method is the entry point in a C# program from where the execution starts. It is always a static method. Overloading of the Main() method is allowed. But in that case, only one Main() method is considered as one entry point to start the execution of the program. 

But remember the method modifier and access modifier cannot be used to overload a method. Please read the method overloading rules here for more details.

The Main() method cannot be overridden because it is the static method and a static method cannot be virtual or abstract.  Please read more about the method overriding here.

Q013. Can you achieve method overriding without inheritance, how?

No, If a method cannot be inherited, it cannot be overridden also. Please read more about the method overriding here.

But shadowing (hiding) is possible without inheritance. Please read more about shadowing here.

Q014. What is the difference between Method Overriding and Method Hiding?

  1. Shadowing is used to protect against subsequent base class modification, while overriding does polymorphism by defining a different implementation.
  2. Shadowing can redefine the entire signature, on the other hand, overriding can redefine only the implementation of a method, not the signature.
  3. For overriding, the base class member must be declared as virtual while it is not mandatory for shadowing.
  4. In shadowing, we can change the access modifier, while we cannot change the access modifier in overriding.
  5. You cannot override a method with a property (or variable), or vice-versa, but you can shadow.
  6. The "override" keyword cannot be used with the "virtual" modifier while the "new" keyword can be used with the "virtual" modifier.
  7. In the case of shadowing, the base class cannot access the shadowed members in the derived class, while in the case of overriding the base class can access the overridden members in the derived class.
  8. There is no control of a base class on shadowing i.e. a base class cannot enforce or prohibit shadowing, but it has control over overriding.
  9. In overriding, a base class reference variable pointing to a child class object will invoke the overridden member in the derived class, not the base class. While in the case of shadowing it will invoke the base class members, not the derived class shadowed members.

Please read the Method Overriding vs Method Hiding article here for more details. 

Q015. We have two methods, one is returning IEnumerable results and the second one is void. Is it an example of Static Polymorphism?

No. Two methods can't be overloaded only based on the return type. Please read more here about return type and overloading rules.

Q016. What is Constructor Overloading in C#? Can you overload a static constructor in C#?

A constructor is nothing but a method without any return type which has the same name as the class or struct name. Constructors are called automatically when we create the object of the class. 

A constructor can also be overloaded just like methods by using different signatures. A class or struct may have multiple constructors that take different arguments.

Constructor overloading is not much different than method overloading. In the case of method overloading we have multiple methods with the same name but different signatures, whereas in Constructor overloading we have multiple constructors with different signatures. 

But the only difference is that Constructor doesn’t have a return type in C#. Constructor overloading is used to pass different parameters based on requirements. Please read the Constructor and Constructor Overloading article here

Static constructors cannot be overloaded because static constructors are always parameterless. Please read the Static Class and Static Constructor article here. Please watch the Static Constructor video here.

Q017. What is the difference between a virtual method and an abstract method?

  1. An abstract method cannot contain body definition while a virtual method must have a body definition.
  2. Overriding all the abstract methods in the inheriting concrete class is mandatory while overriding the virtual methods is optional.
  3. The "abstract" modifier can be used while overriding the abstract method but the "virtual" modifier cannot be used while overriding a virtual method.
  4. An abstract method can only be declared in an abstract class while a virtual method can be defined in a non-abstract class as well.
  5. A virtual method can be overridden to abstract for achieving re-abstraction but an abstract method cannot be overridden to virtual.

Please read the Abstract Method vs Virtual Method article here for more details. 

Q018. What is the difference between inheritance and composition? Why Composition is immune to Fragile Base Class Problem?

Both Inheritance and Composition are used to access the existing methods and properties of other classes. Inheritance derives one class from another, composition defines a class as the sum of its parts. 

Classes and objects created through inheritance are tightly coupled because changing the parent or superclass in an inheritance relationship risks breaking your code. Classes and objects created through composition are loosely coupled, meaning that you can more easily change the component parts without breaking your code. Loosely coupled code offers more flexibility.

Class inheritance is a technique that allows us to create new classes from existing classes. Inheritance contributes a lot to software reuse but also cause tight coupling and fragile base class problem.

Another technique for software reuse is composition. By composition, in our classes under development, we explicitly declare data members as objects of some existing classes.
Composition is generally used when we want the feature of some existing classes inside our new classes, but not their interface i.e we want to use their functionalities to implement features of our new classes.

Please watch the Inheritance vs Composition video here.

Composition is immune to Fragile Base Class Problem because it does not use inheritance. The main reason to use composition is that it allows you to reuse code without modeling an is-a association as you do by using inheritance. 

Association is a relation between two separate classes which establishes through their Objects. In Object-Oriented programming, an Object communicates to other Objects to use functionality and services provided by that object. Composition and Aggregation are the two forms of association.

Please watch the Fragile Base Class Problem video here.

Q019. How can you achieve composition with the abstract classes? 

We can also achieve composition with the abstract class as well. We know that we cannot create the object of an abstract class, but we can create a reference variable of an abstract class. But for that, there should be at least one concrete class that has implemented the abstract class. 

Please watch the Fragile Base Class Problem Solution available in the .Net Framework video here.

Q020. What happens if the inherited interfaces have conflicting method names? or What is explicit interface implementation?

If two interfaces have methods with the same name, we have to implement them explicitly by providing the full name i.e InterfaceName.MethodName. 

In this case, we also need to use the full name for calling that method. If you are not implementing them explicitly, the same method will be called for all the interfaces.

Please watch the Explicit Interface Implementation video here for more details.

To Be Continued Part-03...

Recommended Articles

Thanks for visiting this page. Please follow and join us on LinkedIn, Facebook, Telegram, Quora, YouTube, Twitter, Pinterest, Tumbler, and VK for regular updates.

No comments:

Post a Comment

Please do not enter any HTML. JavaScript or spam link in the comment box.