Skip to main content

Posts

C# : Interview questions (46-50)

    Questions : Explain the pu rpose of the lock statement in C#. What is a sem aphore? How do you handle asynchronous programming in C#? What is the "async" and "await" key words used for? Explain the difference between "Task " and "Thread" in C#. C# : Interview questions (41-45) Answers : Purpose of the lock Statement in C#: The lock statement in C# is used to synchronize access to shared resources in a multithreaded environment. It ensures that only one thread at a time can execute a critical section of code by acquiring an exclusive lock on a specified object, known as a synchronization object or lock object. object lockObject = new object (); lock (lockObject) { // Critical section: Access shared resources } In this example, the lock statement ensures that only one thread can enter the critical section of code at a time by acquiring a lock on the lockObject . Other threads attempting to enter the same critical section will be blo

C# : Interview questions (41-45)

  Questions : What is the purpo se of the Thread class? Explain the difference between a threa d and a process. How do you synchronize threads in C#? What is deadlock in multithre ading? What is a mu tex? C# : Interview questions (36-40) Answers : Purpose of the Thread Class: The Thread class in C# is used to create and control threads, which are independent sequences of execution within a process. Threads allow multiple tasks to execute concurrently, enabling parallelism and multitasking in applications. The Thread class provides methods to start, pause, resume, and terminate threads, as well as facilities for thread synchronization and coordination. Difference between a Thread and a Process: Thread: A thread is the smallest unit of execution within a process. Threads within the same process share the same memory space and resources, including code, data, and file handles. Threads are lightweight compared to processes and have less overhead when switching between them. Threads can co

C# : Interview questions (36-40)

  Questions : How do you implement events in C#? What are lambda exp ressions? Explain the use of LI NQ in C#. What is a lambda expre ssion? How do you creat e a thread in C#? C# : Interview questions (31-35) Answers : Implementing Events in C#: Events in C# are implemented using delegates. Here's how you can declare and raise events in a class: using System; public class Button { public event EventHandler Click; // Declare the event public void OnClick () // Method to raise the event { Click?.Invoke( this , EventArgs.Empty); } } In this example, the Click event is declared as an instance of the EventHandler delegate. The OnClick method raises the event by invoking the delegate with appropriate arguments. Lambda Expressions: Lambda expressions in C# are concise syntax for defining anonymous methods or functions. They allow you to write inline code blocks as arguments to methods or to define delegates or expression trees more compactly. Func&