I saw this code and I was wondering what the Action?
does, after searching I saw this on MSDN "Encapsulates a method that has a single parameter and does not return a value." still unable to understand (application use cases) how to use it, is it a better replacement to ActionResult
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public class PrisonerDilemma
{
public string PrisonerName { get; private set; }
public StrategyBase Strategy { get; private set; }
// What is this Action keyowrd doing?
public Action? LastAction { get; private set; }
public Prisoner(string name, StrategyBase strategy)
{
if (strategy == null)
throw new ArgumentNullException("strategy");
PrisonerName = name;
Strategy = strategy;
}
public void Do(Action? previousActionOfAnotherPrisoner)
{
if (previousActionOfAnotherPrisoner == null)
LastAction = Strategy.Init();
else
LastAction = Strategy.Next(previousActionOfAnotherPrisoner.Value);
}
}
Edit 1: In ASP MVC
- What role does the
Action?
keyword play? and how is it used/leveraged. - If
Action
does NOT take parameters and does NOT return a value, please help explain what is it good for, i.e. when is it typically used* in design patterns? passing/referring controller actions to child actions? - If I wanted to use it as a
Visitor
orStrategy pattern
, can it be passed across object boundaries like in C++? or is the scope restricted to the instance or Class or derived types?
Edit 2: Thanks for the explanation, its now clear that its function reuse. I found another post on SO that helps understand the differences between Action Vs Func, but not the typical use-case application, like whats a good use case to implement this. for e.g. in function reuse, can it be passed across object boundaries like in C++?