C# Interview Questions

Rumman Ansari   2021-02-05   Student   MS dot NET > Interview Questions   774 Share

Constructors

  • A constructor is a special method that is used to initialize objects.
  • The advantage of a constructor, is that it is called when an object of a class is created.
  • It can be used to set initial values for fields.
  • The constructor name must match the class name.
  • It cannot have a return type (like void or int).
  • All classes have constructors by default: if you do not create a class constructor yourself, C# creates one for you. However, then you are not able to set initial values for fields.

Access Modifiers

Modifier

Description

public

The code is accessible for all classes

private

The code is only accessible within the same class

protected

The code is accessible within the same class, or in a class that is inherited from that class.  

internal

The code is only accessible within its own assembly, but not from another assembly.  

Inheritance

  • It is useful for code reusability: reuse fields and methods of an existing class when you create a new class.
  • When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class/super Class, and the new class is referred to as the derived class/sub-Class.
  • The idea of inheritance implements the IS-A For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.

 

Below are the different types of inheritance which is supported by C# in different combinations.

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance (Through Interfaces)
  • Hybrid Inheritance (Through Interfaces)

Method Overloading

  • Method Overloading is the common way of implementing polymorphism.
  • It is the ability to redefine a function in more than one form.
  • A user can implement function overloading by defining two or more functions in a class sharing the same name.
  • C# can distinguish the methods with different method signatures. i.e., the methods can have the same name but with different parameters list (i.e., the number of the parameters, order of the parameters, and data types of the parameters) within the same class.

Operator Overloading

  • Operator overloading is basically the mechanism of providing a special meaning to an ideal C# operator w.r.t. a user-defined data type such as structures or classes.
  • Operator overloading gives the ability to use the same operator to do various operations.
  • Operator overloading gives the ability to use the same operator to do various operations.
  • It is function with special names the keyword operator followed by the symbol for the operator being defined.
  • Only the predefined set of C# operators can be overloaded.

Polymorphism and Overriding Methods

  • Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
  • For example, think of a base class called Animal that has a method called animalSound(). Derived classes of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.)

Abstract Classes and Methods

  • Data abstraction is the process of hiding certain details and showing only essential information to the user.
  • Abstraction can be achieved with either abstract classes or interfaces.
  • The abstract keyword is used for classes and methods:
    • Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
    • Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the derived class (inherited from).

Interfaces

  • An interface is a completely "abstract class", which can only contain abstract methods and properties but not fields/variables.
  • By default, members of an interface are abstract and public.
  • Interface methods do not have a body - the body is provided by the "implement" class.
  • Like abstract classes, interfaces cannot be used to create objects.
  • On implementation of an interface, you must override all of its methods.
  • An interface cannot contain a constructor (as it cannot be used to create objects)

Why and When to Use Interfaces?

  • To achieve security - hide certain details and only show the important details of an object (interface).
  • C# does not support "multiple inheritance" (a class can only inherit from one base class). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma.

 

 

 

Difference between Abstract Class and Interface

Abstract Class

Interface

It contains both declaration and definition part.

It contains only a declaration part.

Multiple inheritance is not achieved by abstract class.

Multiple inheritance is achieved by interface.

It contains constructor.

It does not contain constructor.

It can contain static members.

It does not contain static members.

It can contain different types of access modifiers like public, private, protected etc.

It only contains public access modifier because everything in the interface is public.

The performance of an abstract class is fast.

The performance of interface is slow because it requires time to search actual method in the corresponding class.

It is used to implement the core identity of class.

It is used to implement peripheral abilities of class.

A class can only use one abstract class.

A class can use multiple interfaces.

If many implementations are of the same kind and use common behaviour, then it is superior to use abstract class.

If many implementations only share methods, then it is superior to use Interface.

Abstract class can contain methods, fields, constants, etc.

Interface can only contain methods.

It can be fully, partially or not implemented.

It should be fully implemented.

Exceptions

  • When executing C# code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other uncertain things. When an error occurs, C# will normally stop and generate an error message. The technical term for this is: C# will throw an exception (throw an error)
  • The try statement allows you to define a block of code to be tested for errors while it is being executed.
  • The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
  • The try and catch keywords come in pairs.
  • The throw statement allows you to create a custom error.
  • finally ? The finally block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.

Sr.No.

Exception Class & Description

1

System.IO.IOException

Handles I/O errors.

2

System.IndexOutOfRangeException

Handles errors generated when a method refers to an array index out of range.

3

System.ArrayTypeMismatchException

Handles errors generated when type is mismatched with the array type.

4

System.NullReferenceException

Handles errors generated from referencing a null object.

5

System.DivideByZeroException

Handles errors generated from dividing a dividend with zero.

6

System.InvalidCastException

Handles errors generated during typecasting.

7

System.OutOfMemoryException

Handles errors generated from insufficient free memory.

8

System.StackOverflowException

Handles errors generated from stack overflow.