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

283
Visualizações
Implicit parameter modifiers with delegates? (C#)

I'm working on a library that allows users to instantiate some delegates.

I have a defined a delegate type that deals with a struct as one of its parameters, that we only need to read and not modify, so the in keyword seemed useful.

public struct SomeStruct 
{ 
    public string x; 
    // and possibly more...
}

public delegate void MyDelegate(object foo, in SomeStruct bar);

However, when I go to create it, it tells me I need to put in there.

// Parameter 2 must be declared with the 'in' keyword
MyDelegate x = (foo, bar) => System.Console.WriteLine(bar.x);

If I use in, I now have to explicitly type the parameter...

// doesn't work
MyDelegate x = (foo, in bar) => System.Console.WriteLine(bar.x);

But now that I explicitly type the second parameter, the first parameter needs to be explicit too.

// Inconsistent lambda parameter usage; parameter types must be all explicit or all implicit
MyDelegate x = (foo, in SomeStruct bar) => System.Console.WriteLine(bar.x);
// ok
MyDelegate x = (object foo, in SomeStruct bar) => System.Console.WriteLine(bar.x);

Now in my use case, the types for parameters foo and bar could have long names, generic parameters, etc.

The type information is already there where this delegate type is defined. Is there a way to avoid making my users explicitly type the parameters for this delegate?

I expected (foo, bar) => or (foo, in bar) => to work but it does not.

edit: I was playing around more and this seems to be the case for ref as well, I'm guessing for all parameter modifiers. So question name was changed from just asking about in to modifiers in general

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

No, you can't provide modifiers with implicit anonymous function parameters.

If you look at the grammar in the ECMA standard - which is currently for C# 6, hence the lack of the in modifier - you'll see the difference between explicit_anonymous_function_parameter which includes both an optional modifier and a type, and an implicit_anonymous_function_parameter which is just an identifier:

explicit_anonymous_function_parameter
    : anonymous_function_parameter_modifier? type Identifier
    ;

anonymous_function_parameter_modifier
    : 'ref'
    | 'out'
    ;

implicit_anonymous_function_parameter
    : Identifier
    ;

I agree this can be slightly frustrating - but I wouldn't expect to see it change any time soon.

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