Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
How to add another action to an action in C#?

Why this code will print only "A" and "B", but not "C" ?

Action act = null;
act += () => MessageLog.Message("A");
act += () => MessageLog.Message("B");
Action<Action> add = a => a += () => MessageLog.Message("C");
add(act);
act.Invoke();
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Delegates are immutable. The += operator creates a new delegate with an invocation list consisting of the invocation list from the left side, followed by the invocation list from the right hand side of the operator. It then stores a reference to that delegate in the variable on the left hand side. So

act += () => MessageLog.Message("A");

is equivalent to

act = act + (Action) (() => MessageLog.Message("A"));

which is in turn equivalent to:

act = (Action) Delegate.Combine(act, (Action) (() => MessageLog.Message("A"));

So, now we can come to your add delegate. This lambda expression:

a => a += () => MessageLog.Message("C");

... modifies the parameter a to refer to the newly combined delegate... and then ignores it.

Instead, you need to return the combined delegate (so change add to be a Func<Action, Action>) and then use the return value when you invoke add:

Action act = null;
act += () => MessageLog.Message("A");
act += () => MessageLog.Message("B");
Func<Action, Action> add = a => a + () => MessageLog.Message("C");
act = add(act);
act.Invoke();
over 4 years ago · Santiago Trujillo Relatório

0

As you probably know, A += B is just shorthand for A = A + B, so your add delegate is really just:

a => a = a + () => MessageLog.Message("C")

The + creates the combined delegate a + () => MessageLog.Message("C"), and = assigns it back to a. Notice how you are just reassigning the parameter a, and now it should be clear that this doesn't change the variable that is passed in (act) at all.

Other than returning the combined delegate, you can also pass act by reference:

delegate void ByRefAction<T>(ref T parameter);

...

    Action act = null;
    act += () => Console.WriteLine("A");
    act += () => Console.WriteLine("B");
    ByRefAction<Action> add = (ref Action a) => a += () => Console.WriteLine("C");
    add(ref act);
    act.Invoke();
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda