# Action

***

**1. Assigning an Action to a Variable:**

```csharp
// Declare an Action variable
Action<string> action = (s) => Console.WriteLine(s);

// Invoke the Action
action("Hello World!");
```

**2. Using an Action in a Method Parameter:**

```csharp
public void Method(Action<int> action)
{
    // Invoke the action
    action(10);
}

// Call the method
Method((x) => Console.WriteLine(x * x));
```

**3. Creating an Action Chain:**

```csharp
Action action1 = () => Console.WriteLine("Action 1");
Action action2 = () => Console.WriteLine("Action 2");

// Create an action chain
Action actionChain = action1.AndThen(action2);

// Invoke the action chain
actionChain();
```

**4. Using an Action in a Lambda Expression:**

```csharp
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

// Use an Action in a lambda expression
numbers.ForEach((x) => Console.WriteLine(x));
```

**5. Using an Action in a Delegate:**

```csharp
// Define a delegate
public delegate void MyDelegate(int x);

// Create an Action delegate
MyDelegate myDelegate = (x) => Console.WriteLine(x);

// Invoke the delegate
myDelegate(10);
```

**6. Using an Action in an Event Handler:**

```csharp
public class MyClass
{
    public event Action MyEvent;

    public void RaiseEvent()
    {
        // Invoke the event
        MyEvent?.Invoke();
    }
}

// Subscribe to the event
MyClass myClass = new MyClass();
myClass.MyEvent += () => Console.WriteLine("Event raised!");

// Raise the event
myClass.RaiseEvent();
```

**7. Using an Action in a Timer:**

```csharp
// Create a timer
Timer timer = new Timer((x) => Console.WriteLine("Timer elapsed!"), null, 1000, 1000);

// Start the timer
timer.Start();
```

**8. Using an Action in a Parallel Loop:**

```csharp
// Create a parallel loop
Parallel.For(0, 10, (i) =>
{
    // Perform some action in parallel
    Console.WriteLine("Iteration: " + i);
});
```

**9. Using an Action in a Task:**

```csharp
// Create a task
Task task = new Task(() =>
{
    // Perform some asynchronous action
    Console.WriteLine("Task completed!");
});

// Start the task
task.Start();
```

**10. Using an Action in a Thread:**

```csharp
// Create a thread
Thread thread = new Thread(() =>
{
    // Perform some action in a separate thread
    Console.WriteLine("Thread started!");
});

// Start the thread
thread.Start();
```

**11. Using an Action in a SynchronizationContext:**

```csharp
// Get the current SynchronizationContext
SynchronizationContext context = SynchronizationContext.Current;

// Post an action to the SynchronizationContext
context.Post((x) => Console.WriteLine("Action posted to SynchronizationContext!"), null);
```

**12. Using an Action in a LINQ Query:**

```csharp
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

// Use an Action in a LINQ query
var result = numbers.Select((x) => x * x);

// Print the result
foreach (var item in result)
{
    Console.WriteLine(item);
}
```

**13. Using an Action in a Reactive Extension (Rx)**

```csharp
// Create an Observable sequence
Observable.Create((observer) =>
{
    // Emit some values into the sequence
    observer.OnNext(1);
    observer.OnNext(2);
    observer.OnNext(3);

    // Complete the sequence
    observer.OnCompleted();
})
// Subscribe to the sequence
.Subscribe((x) => Console.WriteLine(x));
```

**14. Using an Action in a Unit Test:**

```csharp
// Create a unit test
[TestClass]
public class UnitTest
{
    [TestMethod]
    public void TestMethod()
    {
        // Define an action to be tested
        Action action = () => Console.WriteLine("Action executed!");

        // Act
        action();

        // Assert
        Assert.IsTrue(true);
    }
}
```

**15. Using an Action in a Web API Controller:**

```csharp
// Define a Web API controller
[ApiController]
[Route("[controller]")]
public class MyController : ControllerBase
{
    [HttpGet]
    public ActionResult Get()
    {
        // Define an action
        Action action = () => Console.WriteLine("Action executed!");

        // Execute the action
        action();

        // Return a result
        return Ok();
    }
}
```

**16. Using an Action in a WinForms Application:**

```csharp
// Create a WinForms application
public class MyForm : Form
{
    public MyForm()
    {
        // Define an action
        Action action = () => Console.WriteLine("Action executed!");

        // Add a button to the form
        Button button = new Button();
        button.Text = "Click me!";
        button.Click += (sender, e) => action();

        // Add the button to the form
        Controls.Add(button);
    }
}
```

**17. Using an Action in a WPF Application:**

```csharp
// Create a WPF application
public partial class MainWindow : Window
{
    public MainWindow()
    {
        // Define an action
        Action action = () => Console.WriteLine("Action executed!");

        // Add a button to the window
        Button button = new Button();
        button.Content = "Click me!";
        button.Click += (sender, e) => action();

        // Add the button to the window
        Content = button;
    }
}
```

**18. Using an Action in a BackgroundWorker:**

```csharp
// Create a BackgroundWorker
BackgroundWorker worker = new BackgroundWorker();

// Define an action to be executed in the background
Action action = () =>
{
    // Perform some action in the background
    Console.WriteLine("Action executed in background!");
};

// Start the BackgroundWorker
worker.DoWork += (sender, e) => action();
worker.RunWorkerAsync();
```

**19. Using an Action in a Task Parallel Library (TPL):**

```csharp
// Create a task
Task task = Task.Factory.StartNew(() =>
{
    // Define an action to be executed in parallel
    Action action = () => Console.WriteLine("Action executed in parallel!");

    // Execute the action
    action();
});
```

**20. Using an Action in a Parallel.Invoke:**

```csharp
// Define an array of actions
Action[] actions = new Action[]
{
    () => Console.WriteLine("Action 1 executed!"),
    () => Console.WriteLine("Action 2 executed!"),
    () => Console.WriteLine("Action 3 executed!")
};

// Execute the actions in parallel
Parallel.Invoke(actions);
```

**21. Using an Action in a Thread Pool:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed in thread pool!");

// Execute the action in the thread pool
ThreadPool.QueueUserWorkItem((x) => action());
```

**22. Using an Action in a Timer with Thread Pool:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed in timer with thread pool!");

// Create a timer with thread pool
Timer timer = new Timer((x) => action(), null, 0, 1000);

// Start the timer
timer.Start();
```

**23. Using an Action in a TaskFactory with ContinueWith:**

```csharp
// Create a task
Task task = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action executed in task!");
})
// Continue with an action when the task completes
.ContinueWith((x) =>
{
    Action action = () => Console.WriteLine("Action executed when task completes!");
    action();
});
```

**24. Using an Action in a TaskFactory with StartNew and TaskContinuationOptions:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed with StartNew and TaskContinuationOptions!");

// Create a task
Task task = Task.Factory.StartNew(() => action(), TaskContinuationOptions.OnlyOnRanToCompletion);
```

**25. Using an Action in a TaskFactory with FromAsync:**

```csharp
// Define a delegate for the asynchronous method
public delegate int AsyncMethod(int x);

// Create an asynchronous method
public int MyAsyncMethod(int x)
{
    // Perform some asynchronous operation
    return x + 1;
}

// Create a task from the asynchronous method
Task<int> task = Task.Factory.FromAsync(MyAsyncMethod, (x, y) => y, 10);

// Continue with an action when the task completes
task.ContinueWith((x) =>
{
    Action action = () => Console.WriteLine($"Action executed when task completes with result: {x.Result}");
    action();
});
```

**26. Using an Action in a TaskFactory with StartNew and Action:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed with StartNew and Action!");

// Create a task
Task task = Task.Factory.StartNew(action, TaskCreationOptions.LongRunning);
```

**27. Using an Action in a TaskFactory with ContinueWhenAll:**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Create a task that continues when both tasks complete
Task task3 = Task.Factory.ContinueWhenAll(new[] { task1, task2 }, (x) =>
{
    Action action = () => Console.WriteLine("Action 3 executed when all tasks complete!");
    action();
});
```

**28. Using an Action in a TaskFactory with ContinueWhenAny:**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Create a task that continues when any task completes
Task task3 = Task.Factory.ContinueWhenAny(new[] { task1, task2 }, (x) =>
{
    Action action = () => Console.WriteLine("Action 3 executed when any task completes!");
    action();
});
```

**29. Using an Action in a TaskFactory with WhenAny and Action:**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Create a task that executes an action when any task completes
Task task3 = task1.WhenAny(task2, (x) =>
{
    Action action = () => Console.WriteLine("Action 3 executed when any task completes!");
    action();
});
```

**30. Using an Action in a TaskFactory with WhenAll and Action:**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Create a task that executes an action when all tasks complete
Task task3 = task1.WhenAll(task2, (x) =>
{
    Action action = () => Console.WriteLine("Action 3 executed when all tasks complete!");
    action();
});
```

**31. Using an Action in a Task.Run:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed with Task.Run()!");

// Run the action in a new task
Task.Run(action);
```

**32. Using an Action in a Task.WaitAll:**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Wait for both tasks to complete
Task.WaitAll(new[] { task1, task2 });

// Execute an action after both tasks complete
Action action = () => Console.WriteLine("Action 3 executed after both tasks complete!");
action();
```

**33. Using an Action in a Task.WaitAny:**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Wait for any of the tasks to complete
Task.WaitAny(new[] { task1, task2 });

// Execute an action after any task completes
Action action = () => Console.WriteLine("Action 3 executed after any task completes!");
action();
```

**34. Using an Action in a Task.FromResult:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed with Task.FromResult()!");

// Create a task from a result
Task<int> task = Task.FromResult(10);

// Continue with the action when the task completes
task.ContinueWith((x) =>
{
    action();
});
```

**35. Using an Action in a Task.Delay:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed after delay!");

// Create a task that delays for 1 second
Task task = Task.Delay(1000);

// Continue with the action when the task completes
task.ContinueWith((x) =>
{
    action();
});
```

**36. Using an Action in a Task.ContinueWith (TaskFactory):**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Continue with the action when task1 completes
Task task2 = task1.ContinueWith((x) =>
{
    Action action = () => Console.WriteLine("Action 2 executed when task1 completes!");
    action();
});
```

**37. Using an Action in a Task.ContinueWithAny (TaskFactory):**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Continue with the action when either task1 or task2 completes
Task task3 = Task.Factory.ContinueAny(new[] { task1, task2 }, (x) =>
{
    Action action = () => Console.WriteLine("Action 3 executed when either task1 or task2 completes!");
    action();
});
```

**38. Using an Action in a Task.ContinueWhenAll (TaskFactory):**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Continue with the action when both task1 and task2 complete
Task task3 = Task.Factory.ContinueWhenAll(new[] { task1, task2 }, (x) =>
{
    Action action = () => Console.WriteLine("Action 3 executed when both task1 and task2 complete!");
    action();
});
```

**39. Using an Action in a Task.ContinueWhenAny (TaskFactory):**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Continue with the action when either task1 or task2 completes
Task task3 = Task.Factory.ContinueWhenAny(new[] { task1, task2 }, (x) =>
{
    Action action = () => Console.WriteLine("Action 3 executed when either task1 or task2 completes!");
    action();
});
```

**40. Using an Action in a Task.FromException:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed after exception!");

// Create a task from an exception
Task task = Task.FromException(new Exception("Error!"));

// Continue with the action when the task completes
task.ContinueWith((x) =>
{
    action();
});
```

**41. Using an Action in a Task.Run (TaskFactory):**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed with Task.Run()!");

// Create a task by running the action
Task task = Task.Run(action);
```

**42. Using an Action in a Task.Run (TaskFactory) with TaskOptions:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed with Task.Run() and TaskOptions!");

// Create a task with TaskOptions
Task task = Task.Run(action, TaskCreationOptions.LongRunning);
```

**43. Using an Action in a Task.Wait:**

```csharp
// Create a task
Task task = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action executed!");
});

// Wait for the task to complete
task.Wait();
```

**44. Using an Action in a Task.Wait (TaskFactory):**

```csharp
// Create a task
Task task = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action executed!");
});

// Wait for the task to complete (TaskFactory)
Task.Wait(task);
```

**45. Using an Action in a Task.Result:**

```csharp
// Create a task
Task<int> task = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action executed!");

    // Return a result
    return 10;
});

// Get the result of the task
int result = task.Result;
```

**46. Using an Action in a Task.WhenAll:**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Create a task that completes when both task1 and task2 complete
Task task3 = Task.WhenAll(new[] { task1, task2 });
```

**47. Using an Action in a Task.WhenAny:**

```csharp
// Create a task
Task task1 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 1 executed!");
});

// Create another task
Task task2 = Task.Factory.StartNew(() =>
{
    // Perform some action
    Console.WriteLine("Action 2 executed!");
});

// Create a task that completes when either task1 or task2 completes
Task task3 = Task.WhenAny(new[] { task1, task2 });
```

**48. Using an Action in a Thread.Sleep:**

```csharp
// Define an action
Action action = () => Console.WriteLine("Action executed after Thread.Sleep()!");

// Sleep the current thread for 1 second
Thread.Sleep(1000);

// Execute the action
action();
```

**49. Using an Action in a Thread.Join:**

```csharp
// Create a thread
Thread thread = new Thread(() =>
{
    // Define an action
    Action action = () => Console.WriteLine("Action executed in thread!");

    // Execute the action
    action();
});

// Start the thread
thread.Start();

// Wait for the thread to complete
thread.Join();
```

**50. Using an Action in a Thread.Abort:**

```csharp
// Create a thread
Thread thread = new Thread(() =>
{
    // Define an action
    Action action = () => Console.WriteLine("Action executed in thread!");

    // Execute the action
    action();
});

// Start the thread
thread.Start();

// Abort the thread
thread.Abort();
```
