C# language interview questions
What is the difference between Interface and Abstract Class?
A class can implement any number of interfaces but a subclass can at most use only one abstract class. An abstract class can have non-abstract methods (concrete methods) while in case of interface all the methods has to be abstract. An abstract class can declare or use any variables while an interface is not allowed to do so. In an abstract class all data member or functions are private by default while in interface all are public, we can't change them manually. In an abstract class we need to use abstract keyword to declare abstract methods while in an interface we don't need to use that. An abstract class can't be used for multiple inheritance while interface can be used as multiple inheritance. An abstract class use constructor while in an interface we don't have any type of constructor.
What is the difference between a struct and a class in C#?
A class is a reference type. A struct is a value type. A class can be used in an inheritance hierarchy. A struct cannot. A class can declare static types. A struct cannot. A struct has a smaller memory footprint than a class.
what is boxing and unboxing?
Boxing the process of taking a value type, putting it inside a new object on the heap, and storing a reference to it on the stack. Unboxing takes the item from the heap and returns a value type that contains the value from the heap.
What is the difference between constant and read only in c#?
Const is static and can only be set at compile time read only can be set at compile time or runtime through a non-static constructor
What is enum in C#?
Enums are enumerated data type in c#. Enums are not for end-user, they are meant for developers. Enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same. Enumerations (enums) make your code much more readable and understandable. Enum values are fixed. Enum can be displayed as a string and processed as an integer. The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong. Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums. Enums are value types and are created on the stack and not on the heap.
What is extension method in c# and how to use them?
Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. An extension method is a special kind of static method, but they are called as if they were instance methods on the extended type.
Define Property in C#.net?
Properties are members that provide a flexible mechanism to read, write or compute the values of private fields, in other words by the property we can access private fields. In other words we can say that a property is a return type function/method with one parameter or without a parameter. These are always public data members. It uses methods to access and assign values to private fields called accessors. We have the three types of properties Read/Write. ReadOnly. WriteOnly
what are accessors?
The get and set portions or blocks of a property are called accessors. These are useful to restrict the accessibility of a property, the set accessor specifies that we can assign a value to a private field in a property and without the set accessor property it is like a read-only field. By the get accessor we can access the value of the private field, in other words it returns a single value. A Get accessor specifies that we can access the value of a field publicly.
Difference between ref vs out vs in ? C#
The out keyword causes arguments to be passed by reference. It is like the ref keyword, except that ref requires that the variable be initialized before it is passed. It is also like the in keyword, except that in does not allow the called method to modify the argument value.
What is the difference between "continue" and "break" statements in C#?
Using break statement, you can 'jump out of a loop' whereas by using continue statement, you can 'jump over one iteration' and then resume your loop execution.
Can "this" be used within a static method?
no this is a key word for current object (which is not static)
What is the difference between ref and out keywords?
ref is two way data flow in and out (pointer) out is one way data flow