Tech Point Fundamentals

Sunday, May 22, 2022

C# Interview Questions and Answers - Part 01

C# Interview Questions and Answers - Part 01


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 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 1st part of this C# Interview Questions and Answers article series. Each part will contain 10 C# Interview Questions with Answers. I will highly recommend to please read the previous parts always before reading the current part.  

C# Interview Questions and Answers - Part 01

Q001. What are the 4 pillars of OOPS?

There are four pillars of any object-oriented programming language:

1. Abstraction:

Abstraction is the process of hiding the internal complex details and showing only the minimum required functionality to the external users. Both abstract class and interface are used for data abstraction. It ensures that only the required information is visible to the user and the rest of the information is hidden from the external world.

Please read the abstract class article hereC#8 interface here, an abstract class vs interface here for more details.

2. Encapsulation:

Encapsulation is the process of binding the data members (properties or variables) to the methods. It wraps the member functions and data members into a single unit. The encapsulation hides or restricts the internal data member from accessing the other external classes. Therefore it is also known as data hiding.  

Encapsulation is achieved by declaring all the variables in the class as private and using properties in the class to set and get the values of variables. Please watch the variable vs properties video here for more details.

3. Inheritance:

Inheritance is a way to access and use the properties and methods of other classes. The original class is known as the base class and the class which inherits the functionality of the base class is the derived class. Inheritance provides code reusability. C# does not support multiple class inheritance due to the diamond problem but multiple interfaces inheritance is allowed.

Please read the Multiple Interface Inheritance and Diamond Problem Resolution in C#8 here for more details.

4. Polymorphism:

The term "polymorphism" means "having many forms". So in OOPS, it means that an object can have multiple functionalities. There are two types of polymorphism in OOPS.

Static Polymorphism or Early Binding involves linking a method with an object during compile time, hence also known as Compile Time BindingMethod Overloading and Operator Overloading are the way to achieve Static Polymorphism. Please read the Static Polymorphism article here for more details

Dynamic Polymorphism or Late Binding involves linking a method with an object during run time, hence also known as Run Time-BindingMethod Overriding is a way to achieve Dynamic Polymorphism. Please read the Dynamic Polymorphism article here for more details.

Q002. What is the difference between Abstraction and Encapsulation?

Both seem very similar but totally different in concept and implementation. In OOPS abstraction hides the code complexity while encapsulation hides the internal working from the outside world.

Abstraction is a way to only show the essential data to the user. It solves an issue at the design level. For abstraction, abstract classes and interfaces are used.

On the other hand, encapsulation hides the code and data into a single entity or unit so that the data can be protected from the outside world. It is an implementation-level process. It is achieved by different access modifiers like private, public, protected. The getters and setters methods of properties are used to hide the data.

Please read more about the abstract class here and abstract class vs interface here.

Q003. What is the difference between Class and Structure in C#?

  1. All the structs are value types while classes are reference types.
  2. Structs cannot have destructors, but classes can have destructors. 
  3. Structs cannot have an explicit default constructor while a class can have a default constructor.
  4. Structs do not support inheritance while classes support inheritance.
  5. Instance field declarations for a struct are not permitted to include variable initializers while a class can initialize the field members.
  6. In C# user can copy one structure object into another one using the assignment operator (=) but a class cannot be.
  7. A structure cannot have the abstract, virtual, or sealed members while a class can have.
  8. A structure cannot have a sealed method but a class can have sealed methods.
  9. A structure can only override the Object class virtual methods while a class can override any base class virtual method and abstract method.

Please read the complete C# Structure article here.

Q004. What are the different Access Modifiers in C#?

Access modifiers specify the accessibility of an object and all of its members. All the C# types (class, interface, structure, etc) have access modifiers implemented, even if they are not stated i.e default access modifiers.

There are six access modifiers in C#, four core types of access modifiers: private, public, protected, internal, and two combined access types: protected-internal and private-protected. 

All the access modifiers are valid for all types and members in all contexts. In some cases, the accessibility of a type member is constrained by the accessibility of its containing type as well.

1. Private: A private type or member can be accessed only by code in the same class or struct. Objects that are specified with private access modifiers are accessible only inside that class, interface, or structure. So nobody can access them outside the declaring class they are created.

2. Public: A public type or member can be accessed by any other code in the same assembly or another assembly that references it. The accessibility level of public members of a type is controlled by the accessibility level of the type itself. Objects that specified public access modifiers are accessible from everywhere in the project and assemblies. So there are no accessibility restrictions at all.

3. Protected: A protected type or member can be accessed only by code in the same class, or in a class that is derived from that class. Objects that specified protected access modifiers are accessible within the declaring class and inheriting class only. So the protected keyword implies that the object is accessible inside the class and in all classes that derive from that class.

4. Internal: A internal type or member can be accessed by any code in the same assembly, but not from another assembly. In other words, internal types or members can be accessed from code that is part of the same compilation. The internal keyword specifies that the object is accessible only inside its own assembly but not in other assemblies. 

5. Protected Internal: A protected internal type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly. The protected internal access modifier is a combination of protected and internal. Therefore it has both features. So you can access a protected internal member only in the same assembly or in a derived class of other assemblies or projects as well.

6. Private Protected: A private protected type or member can be accessed by types derived from the containing classes that are declared within its containing assembly. The private protected access modifier is a combination of private and protected keywords. Therefore it also has both features. So you can access private protected members-only inside the declaring class or in the derived class, but only in the same assembly(project). If you try to access it from another assembly, you will get an error.

Default Accessibility: 

  1. All the Classes, records, and structs declared directly within a namespace(non-nested) can be either public or internal. The internal is the default if no access modifier is specified. 
  2. Class and struct members, including nested classes and structs, have private access by default.
  3. All private nested types aren't accessible from outside the containing type.
  4. Derived classes can't have greater accessibility than their base types. So you can't declare a public class B that derives from an internal class A.
  5. Struct members can't be declared as protected, protected internal, or private protected because structs don't support inheritance.
  6. Normally, the accessibility of a member isn't greater than the accessibility of the type that contains it. However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or override virtual methods that are defined in a public base class.

  7. User-defined operators must always be declared as public and static.
  8. Destructors can't have any accessibility modifiers.
  9. The type of any member field, property, or event must be at least as accessible as the member itself. Similarly, the return type and the parameter types of any method, indexer, or delegate must be at least as accessible as the member itself.
  10. Interfaces declared directly within a namespace can be public or internal and, just like classes and structs, interfaces default to internal access.
  11. Interface members are public by default because the purpose of an interface is to enable other types to access a class or struct. Interface member declarations may also include any access modifier after C#8. 
  12. Enumeration members are always public, and no access modifiers can be applied.
  13. Since delegates behave like classes and structs. So by default, they have internal access when declared directly within a namespace, and private access when nested.

Q005. What is Polymorphism in C#? What are the different types of Polymorphism?

Polymorphism is often referred to as the third pillar of object-oriented programming, after encapsulation and inheritance.  The term "polymorphism" means "having many forms". So in OOPS, it means that an object can have multiple functionalities. 

There are two types of polymorphism in OOPS.

1. Static Polymorphism:

Static Polymorphism or Early Binding involves linking a method with an object during compile time, hence also known as Compile Time Binding. Method Overloading and Operator Overloading are the way to achieve Static Polymorphism. 

Please read the Static Polymorphism article here for more details. You can also read the Static Polymorphism Rules here.

2. Dynamic Polymorphism:

Dynamic Polymorphism or Late Binding involves linking a method with an object during run time, hence also known as Run Time-Binding. Method Overriding is a way to achieve Dynamic Polymorphism. 

At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or arrays. When this polymorphism occurs, the object's declared type is no longer identical to its run-time type.

Please read the Dynamic Polymorphism article here for more details.

Q006. What are the different ways to implement Polymorphism in C#?

Static Polymorphism can be achieved by overloading. If we create two or more members having the same name but different signatures, it is known as member overloading.

Following are the different ways to implement static polymorphism: 
  • Method Overloading
  • Operator Overloading
  • Constructor Overloading
  • Indexer Overloading

Please read about the Static Polymorphism implementation here for more details.

Dynamic Polymorphism can be achieved by method overriding. Base classes may define and implement virtual methods, and derived classes can override them, which means they provide their own definition and implementation. 

At run-time, when the client code calls the method, the CLR looks up the run-time type of the object and invokes that override of the virtual method. In your source code, you can call a method on a base class, and cause a derived class's version of the method to be executed.

Please read the Dynamic Polymorphism implementation article here

Q007. What is the difference between Early Binding and Late Binding in C#? or What is the difference between Compile Time Polymorphism and Run-Time Polymorphism in C#?

In Early Binding, the compiler knows about what kind of object it is, what are all the methods and properties it contains. As soon as you declared the object, .NET Intellisense will populate its methods and properties.  If a method or property does not exist or has data type problems then the compiler automatically throws an exception during compile time.

On the other hand in Late Binding compiler does not know what kind of object it is, what are all the methods and properties it contains. Everything will be known and loaded at the run time. When an object is dynamic or not known which will only bind during runtime is called Late binding. 

Please read about the Early Binding here and Late Binding here for more details.

Q008. What is Shadowing in C#? or  What is the Method Hiding in C#? How can you achieve that?

Shadowing is also known as hiding. In C# you can hide the methods of the base class from the derived class, this concept is known as Method Hiding or Method Shadowing. The term "Shadowing" is normally used in VB.Net which is known as "Hiding" in C#.Net. 

When you say "shadow" you're usually talking about scope while when you say "hiding" you're usually talking about inheritance.

When two-member use the same name, one of them can hide, or shadow, the other one. By shadowing the child class can create its own version of the base class method or property. 

A method of the base class is available to the child class with a new definition or implementation without using the override keyword. 

The "new" keyword is used to perform shadowing, but if it is not used the compiler generates a warning. The compiler hides the method or property of the base class. 

Please read the Shadowing article here for more details.

Q009. What is the abstract method? How an abstract method is different from the virtual method?

A method without body definition and having an "abstract" modifier is known as an abstract method. The keyword "abstract" can only be used just before the return type of the method. Before C# 8 only abstract classes can have abstract methods, but with the introduction of C# 8 interfaces can also have abstract members. 

The abstract modifier indicates that the type being modified has a missing or incomplete implementation. So the inheriting class must have to implement all the abstract members with the "override"  keyword unless the inheriting class is also an abstract class because all the abstract methods are virtual implicitly.

Abstract Method vs Virtual 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 complete article here

Q010. What is the difference between Overloading and Overriding in C#?

  1. Method Overloading is static polymorphism while Method Overriding is dynamic polymorphism. So Method Overloading executes at compile-time while Method Overriding executes at run-time.
  2. Method Overloading is generally defined in the same class while Method Overriding only is used in inheritance.
  3. For Method Overriding the base class method must be virtual (abstract in abstract class) while it is not required in the case of Method Overloading.
  4. In Method Overriding you can not change the method signature in the derived class while Method Overloading is only possible with different method signatures.
  5. Method Overriding requires forcefully rewriting the method definition with the same signature in the derived class while Method Overloading is more flexible by allowing different signatures for different overloading methods.
  6. The Overriding Method is not considered as an overloaded member of the derived class, while other methods with the same name and different signatures are considered as the overloaded methods.

Please read the complete article here for more details.

To Be Continued Part-02...

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.