Home  >  Article  >  Backend Development  >  C# OOP Interview Questions

C# OOP Interview Questions

WBOY
WBOYOriginal
2024-09-03 15:35:40472browse

Introduction to C# OOP Interview Questions

C# is an Object-Oriented, functional, generic, and component-oriented programming language. It has been used to create various applications; it is specifically strong at building Windows desktop applications and games. Web development can also be done efficiently with C#, which has increasingly become popular for mobile development. So, it is an excellent choice for any programmer who wishes to develop web and games. A statically-typed language thoroughly checks the written source code before it becomes an application. It is a complex language to learn, and mastering it can take a substantial amount of time. Developers can use various cross-platform tools to create applications in C# that can be used on mobile and desktop platforms.

If you are looking for a job related to C# OOP, you must prepare for the 2023 C# OOP Interview Questions. Though every interview is different and the job scope is also different, we can help you with the top C# OOP Interview Questions with answers, which will help you take the leap and get you success in your interview.

Part 1 – C# OOP Interview Questions (Basic)

This first part covers essential C# OOP Interview Questions and Answers

1. What is the difference between Interface and Abstract Class?

Answer:

Some difference is listed below:

  • An abstract class can have non-abstract methods (concrete methods), while in the interface case, all the forms must be abstract.
  • An abstract class can declare or use variables, while an interface cannot.
  • In an abstract class, all data members or functions are private by default, while in an interface, all are public; we can’t change them manually.
  • An abstract class uses a constructor, while we don’t have any constructor in an interface.
  • A class can implement any number of interfaces, but a subclass can, at most, use only one abstract class.

2. What are delegates and their uses?

Answer:

A delegate Object is a reference-type variable that holds the reference to a method. The connection can be changed at runtime, which is owned by an object of a delegate. A delegate object can have many functions reference, also known as Invocation List, that refer to tasks in a sequence FIFO; we can new functions ref in this list at runtime by a += operator and remove by -= operator.

Let us move on to the following C# OOP Interview Questions.

3. What is the difference between late binding and early binding?

Answer:

In Compile time polymorphism or Early Binding, we will use multiple methods with the same name but a different parameter type or several parameters. Because of this, we can perform different-different tasks with the same method name in the same class, also known as Method overloading.

public class TestData
{
public int Add(int a, int b, int c)
{
return a + b + c;
}
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
TestData dataClass = new TestData();
int add2 = dataClass.Add(45, 34, 67);
int add1 = dataClass.Add(23, 34);
}
}

Another name for dynamic or runtime polymorphism is late binding.. Here, the method name and the method signature (the number of parameters and parameter type must be the same and may have a different implementation). Method overriding is an example of dynamic polymorphism.

public class Drawing
{
public virtual double Area()
{
return 0;
}
}
public class Square : Drawing
{
public double Length { get; set; }
public Square()
{
Length = 6;
}
public override double Area()
{
return Math.Pow(Length, 2);
}
}
public class Rectangle : Drawing
{
public double Height { get; set; }
public double Width { get; set; }
public Rectangle()
{
Height = 5.3;
Width = 3.4;
}
public override double Area()
{
return Height * Width;
}
}
class Program
{
static void Main(string[] args)
{
Drawing square = new Square();
Console.WriteLine("Area :" + square.Area());
Drawing rectangle = new Rectangle();
Console.WriteLine("Area :" + rectangle.Area());
}
}

4. What would happen in case if the inherited interfaces have conflicting method names?

Answer:

These are some of the typical C# OOP interview questions. Imagine that the same class contains methods that are in conflict. In that case, we can’t implement their body independently in the same class because of the same name and same signature, so we must use the interface name before the method name to remove this method confusion.

interface testInterface1 {<br> void Show();<br> }<br> interface testInterface2 {<br> void Show();<br> }<br> class Abc: testInterface1,<br> testInterface2 {<br> void testInterface1.Show() {<br> Console.WriteLine("For testInterface1 !!");<br> }<br> void testInterface2.Show() {<br> Console.WriteLine("For testInterface2 !!");<br> }<br> }<br>

Part 2 – C# OOP Interview Questions (Advanced)

This first part covers advanced C# OOP Interview Questions and answers

6. What is an accessibility modifier, and how many are there in C#?

Answer:

Access modifiers are keywords used to specify the declared accessibility of a member or a type. In C#, there are 5 several types of Access Modifiers.

Public – No restrictions on accessing public members.

Private – limited access within a class definition; if none is specified, then this is the default.

Protected – access is limited to within class definition and any class that inherits from the course.

Internal – access is limited exclusively to the classes defined by the current project.

7. What is a virtual method in C#?

Answer:

Developers use a virtual method to define a method that can be redefined in derived classes. We make a virtual method in the base class using the virtual keyword, and that method is overridden in the derived class using the override keyword.

Let us move on to the following C# OOP Interview Questions.

8. How to avoid NULL in C#?

Answer:

Null is not an object. We can have a class, but a variable with a NULL value is not pointing to any object. We might come across a piece of code that contains many conditional statements that check if the value of a variable is NULL. Let’s review a static method:

public static string DisplayUpperString( string s ){
string upper = string.Empty;
If( s == null ) {
upper = null;
}
else {
upper = s.ToUpper();
}
return upper;
}

This code is fine and converts to the upper case of a given string.
But from an OO perspective, consider constructing an object that represents nothing rather than evaluating it for NULL.

public static String DisplayUpperString ( PossibleString s ){
string upper = s.ToPossibleUpper();
return upper;
}

Now the function is less cluttered, more readable, and no longer uses the check for a NULL value.

9. What is the extension method in C#, and how to use them?

Answer:

Interviewers frequently ask about extension methods in C# OOP interviews. This method enables you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are only in scope when explicitly importing the namespace into your source code using a directive.

10. Can “this” keyword be used within a static method?

Answer:

Since “this” keyword returns a reference to the current instance of a class, we cannot use this inside a static method. And static members exist without any instance of the class and call with the name of the class, not by instance. The “this” keyword is implicitly defined in every constructor and non-static method as a particular kind of reference variable as the first argument of the class type in which it is defined.

The above is the detailed content of C# OOP Interview Questions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn