C#:
In this blog we can see top 15 interview Questions in C#.
1.What are Properties in C#?
A property in C# is a class member that offers a method to read, write, or calculate the value of a private field. It provides a public interface for the access and modification of data held by a class while yet preserving the class’s control over that data’s access and manipulation.
2. What is an extension method in C#?
A static method known as an extension method in C# is used to increase the capability of an existing type without changing the original type or producing a new derived type. Developers can add new methods to existing kinds, such as classes, structs, interfaces, enums, etc., that were not originally defined in those types by using extension methods.
3.What is the difference between Dispose and Finalize in C#?
Resources can be released in C# using both the Dispose and Finalize methods, although these two have different functions and behaviours.
When using the Dispose method, unmanaged resources that aren’t automatically handled by the.NET runtime are released, including file handles or database connections. The Dispose method, which is defined by the IDisposable interface, is normally implemented in a class that implements this interface.
But before an object is garbage collected, cleanup tasks are carried out on it using the Finalize method. Because of this, it is frequently implemented in a class that overrides the Object. Decide on a strategy.
4.What is the difference between String and StringBuilder in C#?
Both StringBuilder and string are commonly used to create strings of values, although they differ significantly in terms of instance generation and efficiency.
String :
An immutable object is a string. When string objects are created in code, they are immutable, meaning that they cannot be changed or modified in any way, including by adding new values or replacing or appending current values. When we need to edit a string, we simply dispose of the old value of the string object and build a new instance in memory to contain the updated string object value.
StringBuilder:
System. The string value is stored in the mutable object Text.StringBuilder, which implies that once a System is created, it can be changed. Stringbuilder object for text. This object can be used for any activity, such as adding or updating data without establishing a new instance of the System, or inserting a value into an existing string using insert methods. Every time, Text.StringBuilder is used, which means the previous object is being used. As a result, it operates more quickly than the System. String. To better grasp System, let’s look at an example. Text.StringBuilder.
5. What is the use of a delegate in C#?
As in C++ (the explanation of which is outside the purview of this article), a delegate is an abstraction of one or more function pointers. Delegates are how the.NET has implemented the idea of function pointers. You can treat a function like data using delegates. Delegates enable functions to be stored in an array, returned as a value from a function, and supplied as parameters.
6. What are sealed classes in C#?
The inheritance aspect of object-oriented programming is constrained by the use of sealed classes. Once a class is designated as a sealed class, inheritance is not permitted.
The sealed modifier in C# designates a class as sealed. The sealed class function in Visual Basic.NET is accomplished through the Not Inheritable keyword. A class that derives from a sealed class causes the compiler to throw an error.
7.What are partial classes?
A partial class is only employed to divide a class’ definition into two or more classes inside the same source code file or in different source files. A class definition can be created in many files, which will be combined at run time to form a single class. Also, when you create an instance of this class, you may use the same object to access all the methods across all source files.
8.What is the difference between boxing and unboxing in C#?
Both boxing and unboxing are used for type conversion, although there are some distinctions between the two:
Boxing :
Boxing is the process of changing a value type data type into an object or any interface data type that this value type implements. For instance, when the CLR boxes a value, it indicates that the value is wrapped inside a System.Object and stored on the memory region in the application domain when the CLR changes a value type to an Object Type.
Unboxing :
The process of “unboxing” can also be used to separate an implemented interface type’s value type from an object. Unboxing must be done explicitly via code as opposed to boxing, which can be done implicitly.
9.What is IEnumerable<> in C#?The parent interface of all System’s non-generic collections is IEnumerable. collections with enumerable namespaces, such as ArrayList and HastTable. The parent interface of all generic collections classes in the System, IEnumerableT>, is the generic equivalent of this interface. Collections. List and other generic namespaces are examples.
10. What is the difference between late and early binding in C#?
Early Binding and Late Binding concepts belong to polymorphism in C#. Polymorphism is the feature of object-oriented programming that allows a language to use the same name in different forms. For example, a method named Add can add integers, doubles, and decimals.
Polymorphism we have two different types to achieve:
Compile Time is also known as Early Binding or Overloading.
Run Time is also known as Late Binding or Overriding.
Compile Time Polymorphism or Early Binding** ** :
In Compile time polymorphism or Early Binding, we will use multiple methods with the same name but different types of parameters or maybe the number of parameters. Because of this, we can perform different-different tasks with the same method name in the same class, also known as Method overloading.
Run Time Polymorphism or Late Binding :
Run time polymorphism is also known as late binding. In Run Time Polymorphism or Late Binding, we can use the same method names with the same signatures, which means the same type or number of parameters, but not in the same class because the compiler doesn’t allow that at compile time. Therefore, we can use that bind at run time in the derived class when a child or derived class object is instantiated. That’s why we call it Late Binding. We have to create my parent class functions as partial and in the driver or child class as override functions with the override keyword.
11.What are the differences between IEnumerable and IQueryable?
Let’s first define IEnumerable and IQueryable before discussing their differences.
IEnumerable :
All of the non-generic collections in System have it as their parent interface. collections with enumerable namespaces, such as ArrayList and HastTable. The parent interface of all generic collections classes in the System, IEnumerableT>, is the generic equivalent of this interface. List> and other namespaces under Collections.Generic.
IQueryable :
The IQueryable interface is meant to be used by query providers, according to MSDN. As a result, only providers that also implement IQueryableT> should use it. The standard query operators cannot be used on the provider’s data source unless the provider also implements IQueryableT>.
12.What happens if the inherited interfaces have conflicting method names?
If we implement numerous interfaces in the same class with conflicting method names, we don’t need to define them all. To put it another way, if two methods in the same class have the same name and signature, their bodies cannot be implemented independently in the same class. In order to remove this method confiscation, we must put the interface name before the method name.
13.What are the Arrays in C#?
An array index in C# begins at zero. It indicates that the first item in an array begins at position 0. The location of the last item in an array will therefore equal the total number of items minus 1. As a result, the ninth item in an array of ten elements is the previous item in the array.
14.What is the Constructor Chaining in C#?
Constructor chaining is a technique for tying together two or more classes in an inheritance relationship. When using function Object() { [native code] } chaining, the base keyword implicitly maps each child class function Object() { [native code] } to a parent class function Object() { [native code] }, causing the child class’s function Object() { [native code] } to be called whenever an instance of the child class is created. Inheritance is impossible without it. How does Cconstructor #’s chaining work?
Constructor chaining is a technique for tying together two or more classes in an inheritance relationship. When using function Object() { [native code] } chaining, the base keyword implicitly maps each child class function Object() { [native code] } to a parent class function Object() { [native code] }, causing the child class’s function Object() { [native code] } to be called whenever an instance of the child class is created. Inheritance is impossible without it.
15.What is the Constructor Chaining in C#?
Constructor chaining allows you to link two or more classes together in an inheritance connection. Every child class function Object() { [native code] } in function Object() { [native code] } chaining is implicitly mapped by the base keyword to a parent class function Object() { [native code] }, so whenever you create an instance of the child class, it will call the function Object() { [native code] } of the parent class. It is necessary for inheritance to be feasible.
For any Help or Queries Contact us on info@crmonce.com or +918096556344