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

278
Visualizações
Convert list to params C#

I have this following list:

var myList = new List<KeyValuePair<string, object>>();

And this function:

public void Test(params KeyValuePair<string, object>[] list)

How can I do a conversion of the list to params when using the function? Like that:

Test(myList);
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You method declaration KeyValuePair<string, object>[] list states that it will accept an array so you need to convert your list to array like this

Test(myList.ToArray());
about 4 years ago · Santiago Trujillo Relatório

0

Given the original question, I think it's worth clarifying how the params keyword functions. It has to be a decorator on a 1D array, and simply allows an alternate syntax for calling the function.

For example if we have a string concatenation function:

public string Test(string[] names) {
    return String.Join(",", names);
}

This can be called using

Test(new[] { "Fred", "Wilma", "Pebbles"});

but the inclusion of the params keyword simply allows a friendlier looking calling syntax:

public string Test(params string[] names) {
    return String.Join(",", names);
}

Test("Fred", "Wilma", "Pebbles");

I'm not a huge fan of the params keyword, but I do sometimes use it to make calling code more readable (as in this example).

So when the OP asks "How can I do a conversion of the list to params?", the underlying truth is that you don't, as params is not a type and is simply a "syntactical sugar" feature to give an alternative syntax for passing in an array.

about 4 years ago · Santiago Trujillo Relatório

0

You should convert the list to form which is accepted as parameter. In this case convert List to Array

Hence,

var arrayParam = mylist.ToArray();
Test(arrayParam);
about 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