Question: What is the difference between 'IEnumerable' and 'IQueryable' in C#?
|
Answer: The 'IEnumerable' represents a collection that can be enumerated using foreach,
but it operates on an in-memory collection.
And 'IQueryable', on the other hand, represents a query that can be executed on a data source,
typically a database, allowing for deferred execution and query optimization.
|
Question: What is boxing and unboxing in C#?
|
Answer: In C# boxing is the process of converting a value type to a reference type, while unboxing is the process of converting a boxed value type back to its original value type.
Boxing occurs implicitly when a value type is assigned to a variable of a reference type, while unboxing requires an explicit cast.
|
Question: What is the use of 'async' and 'await' keywords in C#?
|
Answer: The 'async' and 'await' are used for asynchronous programming in C#.
'async' marks a method as asynchronous, allowing it to use the 'await' keyword to asynchronously
wait for the completion of asynchronous operations without blocking the thread.
|
Question: Can you explain the purpose of the 'lock' statement in C#?
|
Answer: The 'lock' statement is used to synchronize access to shared resources
in a multithreaded environment. It acquires an exclusive lock on a specified object, preventing
other threads from accessing the locked code block concurrently.
|
Question: What is a lambda expression in C#?
|
Answer: A lambda expression is a concise way to represent an anonymous function in C#.
It allows for the creation of delegate instances or expression trees without explicitly defining a named method.
|
Question: What is a constructor in C#?
|
Answer: A constructor is a special method in a class that is called when an instance
of the class is created. It is used to initialize the object's state and perform any necessary setup tasks.
|
Question: What is the difference between 'throw' and 'throw ex' in C# exception handling?
|
Answer: The 'throw' without the 'ex' rethrows the current exception while preserving the original stack trace.
Whereas 'throw ex' replaces the current exception with a new one, resetting the stack trace.
It is generally recommended to use 'throw' without 'ex' to preserve the original exception information.
|
Question: What are attributes in C#?
|
Answer: Attributes provide a way to add metadata, such as additional information or behavior,
to types and members in C#. They are used for a variety of purposes, including serialization,
code generation, and declarative programming.
|