Interview questions for C# language


Q1: What is the difference between a struct and a class in C#?

Ans: Class and Struct are both user-defined data types but have certain important distinctions:

Struct

  • The Struct is inherited from the System and the value type of C#.Value Type.
  • For lesser quantities of data, Struct is generally utilized.
  • Other types cannot inherit Struct.
  • There can be no abstract structure.
  • You don't require a new keyword to build an object.
  • No default builder is permitted to create.

Class

  • The class is inherited from the System through a reference type in C#. Object Type.
  • For huge volumes of data, classes are generally utilized.
  • Classes from other classes can be inherited.
  • An abstract kind can be a class.
  • A default builder can be created

Q2: What is garbage collection in C#?

Ans: Collection of garbage is the process of clearing the memory collected by undesirable things. When a class object is created, the object in the heap memory will automatically have a certain memory space. After all the operations are performed on the item, the object's memory space becomes a waste. Memory needs to be free. In three instances, garbage collecting occurs:

  • The class is inherited from the System through a reference type in C#. Object Type.
  • For huge volumes of data, classes are generally utilized.
  • Classes from other classes can be inherited.
  • An abstract kind can be a class.
  • A default builder can be created.

Q3: What are extension methods in C#?

Ans: Extension methods allow you to add methods to existing types without creating, compiling, or other changes to the original type. An extension method is a particular static method, yet it is called as if an extension method were instance methods.

An extension's static method is a class method that uses the "this" modifier for the first parameter. The first parameter type is the extended parameter type. Extension methods are only available if the namespace is explicitly imported using a directive into your source code.

Q4: How is C# different from C?

Ans: You always know C is the language of the proceeding, whereas C# is a more objective language. The major distinction is that C# automatically supports Common Language Runtime (CLR) garbage collection, while C does not. C# mostly needs a .NET framework to run, whereas C is a language for platform diagnostics.

Q5: What is the difference between an abstract class and an interface?

Ans: Let's examine the distinctions between an abstract and an interface class:

Abstract classes are not instantiable classes, i.e., cannot build an object. The interface is like an abstract class because abstract methods are contained within the interface.

Surprisingly, abstract and non-abstract methods can be used in abstract classes, although all interface methods are abstract.

Since abstract classes might have abstract and non-abstract methods, we must use the keyword Abstract to specify abstract methods. There's no such necessity on the interface, however.

An abstract class has constructors, while an interface encompasses none.

Example

Abstract class: public abstract class Shape{ public abstract void draw(); } Interface: public interface Paintable{ void paint(); }