In this Blog We will see the  top 10 interview questions in C#.

1. How is C# different from C?

C# is more object-oriented than C, which is a procedural language that is well known. The primary distinction is that C# and C do not support Common Language Runtime’s (CLR) automatic garbage collection. While C is a platform-independent language, C# mostly requires the.NET framework to run.

2. What is Common Language Runtime (CLR)?

CLR manages C# programme execution among other languages. Memory management, trash collection, and security handling are handled by the CLR architecture.

3. What is garbage collection in C#?

The process of cleaning up memory that has been occupied by undesired items is known as garbage collection. A certain amount of memory is automatically assigned to a class object when it is created in the heap memory. Now that you’ve finished working on the object, the memory space it formerly occupied is wasted. Memory has to be freed up.

Garbage collection happens in three cases:

1.If the occupied memory by the objects exceeds the pre-set threshold value.
2.If the garbage collection method is called
3.If your system has low physical memory.

4. What are the types of classes in C#?

Class is an entity that encapsulates all the properties of its objects and instances as a single unit. C# has four types of such classes:

Static class : Inheritance is prohibited by the static class, which is declared by the keyword. Since static classes cannot have objects, you cannot create them.

Partial class: The class named “partial” enables its members to divide or distribute source (.cs) files in portions.

Abstract class : Abstract classes are those that cannot be instantiated and thus cannot be used to create objects. Abstract classes implement the OOPS abstraction concept. Abstraction aids in extracting important details while concealing irrelevant ones.

Sealed class : Sealed classes are classes that cannot be inherited. Use the keyword sealed to restrict access to users to inherit that class. 

5. What is a managed and unmanaged code?

Managed code allows you to run code in the.NET framework on a managed CLR runtime environment.
Managed code executes in a different environment than the operating system.

Benefits: Provides various services like a garbage collector, exception handling, etc.

Unmanaged code is code that does not run on the CLR and operates outside of the.NET framework.
They do not provide high-level language services and thus operate without them. C++ is one such example.

6. What is the difference between an abstract class and an interface?

Let’s dig into the differences between an abstract class and an interface:

1.Abstract classes are classes that cannot be instantiated ie. that cannot create an object. The interface is like an abstract class because all the methods inside the interface are abstract methods.
2.Surprisingly, abstract classes can have both abstract and non-abstract methods but all the methods of an interface are abstract methods.
3.Since abstract classes can have both abstract and non-abstract methods, we need to use the Abstract keyword to declare abstract methods. But in the interface, there is no such need.
An abstract class has constructors while an interface encompasses none.

7. What are the differences between ref and out keywords?

C# ref keywords pass arguments by reference and not value. To use the ‘ref’ keyword, you need to explicitly mention ‘ref’.
C# out keywords pass arguments within methods and functions.

8. What are extension methods in C#?

Extension techniques enable the addition of additional techniques to already-existing ones. Static methods have been added. When you wish to add methods to an existing class but don’t have the authority to do so, you can occasionally create a new static class that contains those new methods. Bind this class to the existing one once the extended methods have been declared to see how the methods will be added to the existing class.

9. What are Generics in C#?

The fundamental principle of type-safety in C# is compromised since specifying any type of object is considered permissible in collections. Generics were therefore added to the code to type-safe it by permitting reuse of the data processing algorithms. Generics in C# denote a lack of connection to any particular data type. The burden of using boxed, unboxed, and typecast objects is lessened by generics. Generic definitions are always included in angular brackets (>). Making a generic class involves.

10. What is the difference between an Array and ArrayList in C#?

An array is a grouping of related variables with a single common name. While an array list is a group of separately indexable objects. You can access a variety of capabilities with ArrayList, including adding, searching, and sorting objects in the ArrayList as well as dynamic memory allocation.

  • When declaring an array the size of the items is fixed therefore, the memory allocation is fixed. But with ArrayList, it can be increased or decreased dynamically.
  • Array belongs to system.array namespace while ArrayList belongs to the system.collection namespace.
  • All items in an array are of the same datatype while all the items in an ArrayList can be of the same or different data types.
  • While arrays cannot accept null, ArrayList can accept null values.

 

For any Help or Queries Contact us on info@crmonce.com or +918096556344