文章目录
- 1. What is the difference between “dispose” and “finalize” variables in C#?
- 2. What does “Jagged Arrays” mean?
- 3. What is a Class and an Object?
- 4. Explain Code compilation in C#
- 5. What is the difference between Virtual method and Abstract method?
- 6. What is a Hashtable in C#?
- 7. What is LINQ in C#?
- 8. What are Indexers in C#?
- 9. Difference between the Equality Operator (==) and Equals() Method in C#
- 10. What's the Difference between the Is and As operator in C#
- 11. What are Different Ways a Method can be Overloaded?
- 12. What is Serialization?
- 13. What are the different types of Delegates?
- 14. How to use extension methods?
- 15. What is the difference between String and StringBuilder in C#?
- 16. What are sealed classes in C#?
- 17. What is a Delegate? Explain.
- 18. What are Events?
- 19. How to use Delegates with Events?
- 20. What do Multicast Delegates mean?
- 21. Explain Publishers and Subscribers in Events.
- 22. What are Synchronous and Asynchronous operations?
- 23. What is Reflection in C#?
- 24. What is a Generic Class?
- 25. What is a Jagged Array?
- 26. What is an Array Class?
- 27. What is a String? What are the properties of a String Class?
- 28. Name some properties of Thread Class.
- 29. What are the different states of a Thread?
- 30. What are Async and Await?
- 31. Async Keyword
- 32. What is a Deadlock?
- 33. Explain Lock, Monitors, and Mutex Object in Threading.
- 34. What is a Race Condition?
- 35. What is Thread Pooling?
- 36. What is Serialization?
- 37. What are the types of Serialization?
- 38. What is an XSD file?
- 39. What is an Object and a Class?
- 40. What are the fundamental OOP concepts?
- 41. What is Managed and Unmanaged code?
- 42. What is an Interface?
- 43. Explain Get and Set Accessor properties?
- 44. What is a Thread? What is Multithreading?
- 45. What is “using” statement in C#?
- 46. Explain Abstraction.
- 47. What are C# I/O classes? What are the commonly used I/O classes?
- 48. What is StreamReader/StreamWriter class?
- 49. What is a Destructor in C#?
- 50. What is an Abstract Class?
- 51. How can one use nullable types in .Net?
- 52. How to use nullable types in .Net?
- 53. What is the difference between Continue and Break Statement?
- 54. What is the difference between finally and finalize block?
- 55. What is an Array? Give the syntax for a single and multi-dimensional array?
- 56. What are Boxing and Unboxing?
- 57. What is an Array used for?
- 58. What is an Escape Sequence? Name the sequences in C
- 59. What are the basic String Operations?
- 60. What is an Escape Sequence? Name some String escape sequences in C#.
- 61. In C#.Net, what do indexers mean?
- 62. What are Regular expressions? Search a string using regular expressions?
- 63. State the difference between direct cast and ctype.
- 64. How can one use the singleton design pattern in C#?
- 65. What are the basic String Operations? Explain.
- 66. What is Parsing? How to Parse a Date Time String?
- 67. What are extension methods in C#?
- 68. What is the difference between the Virtual method and the Abstract method?
- 69. What are the types of errors in C#?
- 70. What is the difference between method overriding and method overloading?
- 71. Why can't you specify the accessibility modifier for methods inside the interface?
- 72. What is an object pool in .NET?
- 73. What is the difference between read-only and constant?
- 74. What are partial classes?
- 75. Can “this” be used in a static method?
- 76. What is IEnumerable<> in C#?
- 77. What is the difference between late binding and early binding in C#?
- 78. What is the Constructor Chaining in C#?
- 79. What’s the difference between the Array.CopyTo() and Array.Clone()?
- 80. Can Multiple Catch Blocks be executed in C#?
- 81. What are Value types and Reference types in C#?
- 82. How do you use the “using” statement in C#?
- 83. What are Anonymous Types in C#?
- 84. Explain “static” keyword in C#?
- 85. What are the different types of classes in C#?
- 86. What is Managed and Unmanaged code?
- 87. Explain Namespaces in C#
- 88. Explain Polymorphism
- 89. How is Exception Handling implemented in C#?
https://jackniu81.github.io/2021/04/22/2021-Top-100-C-NET-Interview-Questions-And-Answers/
1. What is the difference between “dispose” and “finalize” variables in C#?
- Dispose - This method uses interface – “IDisposable” interface and it will free up both managed and unmanaged codes like – database connection, files etc.
- Finalize - This method is called internally unlike Dispose method which is called explicitly. It is called by garbage collector and can’t be called from the code.
2. What does “Jagged Arrays” mean?
Answer: Jagged arrays refer to the array that has an element of type array. The dimensions and the sizes of the elements are different. An array of arrays is the other name of the jagged array.
3. What is a Class and an Object?
A ‘Class’ is an encapsulation of methods and properties that are used to represent an entity in real-time. Class brings all of the instances together in a single unit. An ‘Object’ is an instance of a Class, or a block of allocated memory that can be stored in the form of Variables, Array or a Collection.
4. Explain Code compilation in C#
Code compilation has four steps which include:
- Compiling source code to Managed code by C# compiler
- Executing the assembly by CLR
- Combining the new code into assemblies
- Loading the Common Language Runtime (CLR)
5. What is the difference between Virtual method and Abstract method?
A Virtual method must always have a default implementation. An Abstract method does not have an implementation. An override keyword is not necessary here, though it can be used.
6. What is a Hashtable in C#?
A Hashtable is a collection that stores (Keys, Values) pairs. Here, the Keys are used to find the storage location and is immutable and cannot have duplicate entries in a Hashtable. The .Net Framework has provided a Hash Table class that contains all the functionality required to implement a hash table without any additional development. The hash table is a general-purpose dictionary collection. Each item within the collection is a DictionaryEntry object with two properties: a key object and a value object. These are known as Key/Value. When items are added to a hash table, a hash code is generated automatically. This code is hidden from the developer. Access to the table’s values is achieved using the key object for identification. As the items in the collection are sorted according to the hidden hash code, the items should be considered to be randomly ordered.
7. What is LINQ in C#?
LINQ stands for Language Integrated Query. LINQ is a data querying methodology that provides querying capabilities to .NET languages with a syntax similar to a SQL query.
LINQ has a great power of querying on any source of data. The data source could be collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable interface.
Advantages of LINQ
- LINQ offers an object-based, language-integrated way to query over data no matter where that data came from. So through LINQ, we can query a database and XML as well as collections.
- Compile-time syntax checking.
8. What are Indexers in C#?
C# introduces a new concept known as Indexers which are used for treating an object as an array. The indexers are usually known as smart arrays in C#. They are not an essential part of object-oriented programming.
Defining an indexer allows you to create classes that act as virtual arrays. Instances of that class can be accessed using the [] array access operator.
9. Difference between the Equality Operator (==) and Equals() Method in C#
Both the == Operator and the Equals() method are used to compare two value type data items or reference type data items. The Equality Operator (==) is the comparison operator and the Equals() method compares the contents of a string. The == Operator compares the reference identity while the Equals() method compares only contents.
10. What’s the Difference between the Is and As operator in C#
-
“is” operator: In C# language, we use the “is” operator to check the object type. If two objects are of the same type, it returns true, else it returns false.
-
“as” operator: The “as” operator behaves in a similar way as the “is” operator. The only difference is it returns the object if both are compatible with that type. Else it returns a null.
11. What are Different Ways a Method can be Overloaded?
Method overloading is a way to achieve compile-time polymorphism where we can use a method with the same name but different signatures. For example, the following code example has a method volume with three different signatures based on the number and type of parameters and return values.
12. What is Serialization?
Serialization converts a code to its binary format using a process. After it is converted to bytes, it can be easily stored and written to a disk. Serializations are useful so that the original form of the code isn’t lost and it can be retrieved later on.
13. What are the different types of Delegates?
The different types of Delegates are: Single Delegate, Multicast Delegate and Generic Delegate.
14. How to use extension methods?
An extension method is a static method of a static class, where the “this” modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.
Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.
15. What is the difference between String and StringBuilder in C#?
StringBuilder and string are both used to string values, but both have many differences on the bases of instance creation and also in performance.
16. What are sealed classes in C#?
Sealed classes are used to restrict the inheritance feature of object-oriented programming. Once a class is defined as a sealed class, the class cannot be inherited.
In C#, the sealed modifier is used to define a class as sealed. In Visual Basic .NET the Not Inheritable keyword serves the purpose of the sealed class. If a class is derived from a sealed class then the compiler throws an error.
If you have ever noticed, structs are sealed. You cannot derive a class from a struct.
17. What is a Delegate? Explain.
A Delegate is a variable that holds the reference to a method. Hence it is a function pointer or reference type. All Delegates are derived from System.Delegate namespace. Both Delegate and the method that it refers to can have the same signature.
Declaring a delegate: public delegate void AddNumbers(int n);
After the declaration of a delegate, the object must be created by the delegate using the new keyword.
AddNumbers an1 = new AddNumbers(number);
The delegate provides a kind of encapsulation to the reference method, which will internally get called when a delegate is called.
public delegate int myDel(int number);
public class Program
{
public int AddNumbers(int a)
{
int Sum = a + 10;
return Sum;
}
public void Start()
{
myDel DelgateExample = AddNumbers;
}
}
In the above example, we have a delegate myDel which takes an integer value as a parameter. Class Program has a method of the same signature as the delegate, called AddNumbers().
If there is another method called Start() which creates an object of the delegate, then the object can be assigned to AddNumbers as it has the same signature as that of the delegate.
18. What are Events?
Events are user actions that generate notifications to the application to which it must respond. The user actions can be mouse movements, keypress and so on.
Programmatically, a class that raises an event is called a publisher and a class which responds/receives the event is called a subscriber. Event should have at least one subscriber else that event is never raised.
Delegates are used to declare Events.
Public delegate void PrintNumbers();
Event PrintNumbers myEvent;
19. How to use Delegates with Events?
Delegates are used to raise events and handle them. Always a delegate needs to be declared first and then the Events are declared.
20. What do Multicast Delegates mean?
A Delegate that points to more than one method is called a Multicast Delegate. Multicasting is achieved by using + and += operator.
21. Explain Publishers and Subscribers in Events.
Publisher is a class responsible for publishing a message of different types of other classes. The message is nothing but Event as discussed in the above questions.
From the Example in Q #32, Class Patient is the Publisher class. It is generating an Event deathEvent, which is received by the other classes.
Subscribers capture the message of the type that it is interested in. Again, from the Example of Q#32, Class Insurance and Bank are Subscribers. They are interested in event deathEvent of type void.
22. What are Synchronous and Asynchronous operations?
Synchronization is a way to create a thread-safe code where only one thread can access the resource at any given time. The asynchronous call waits for the method to complete before continuing with the program flow.
Synchronous programming badly affects the UI operations when the user tries to perform time-consuming operations since only one thread will be used. In Asynchronous operation, the method call will immediately return so that the program can perform other operations while the called method completes its work in certain situations.
In C#, Async and Await keywords are used to achieve asynchronous programming. Look at Q #43 for more details on synchronous programming.