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

77
Visualizações
Creating an array of System.ValueTuple in c# 7

In my code I have:

private static readonly ValueTuple<string, string>[] test = {("foo", "bar"), ("baz", "foz")};

But when I compile my code, I get:

TypoGenerator.cs(52,76):  error CS1026: Unexpected symbol `,', expecting `)'
TypoGenerator.cs(52,84):  error CS1026: Unexpected symbol `)', expecting `)'
TypoGenerator.cs(52,94):  error CS1026: Unexpected symbol `,', expecting `)'
TypoGenerator.cs(52,103):  error CS1026: Unexpected symbol `)', expecting `)'
TypoGenerator.cs(117,42):  error CS1525: Unexpected symbol `('
TypoGenerator.cs(117,58):  error CS1525: Unexpected symbol `['

What is the correct way to create and initialize an array of ValueTuples?

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

0

Try to create an instance of array with new and instances of tuple with new keyword

private static readonly ValueTuple<string, string>[] test = new ValueTuple<string, string>[]{
        new ValueTuple<string, string>("foo", "bar"), 
        new ValueTuple<string, string>("baz", "foz")
};

or with C#7 tuple syntax

private static readonly ValueTuple<string, string>[] test = new ValueTuple<string, string>[]{
        ("foo", "bar"), 
        ("baz", "foz")
};

Update:

Right now all declarations from the question and this answer works fine with Rider 2017.1 build #RD-171.4456.1432 and .NET Core 1.0.4. The simplest one is that @ZevSpitz mentioned in comments and it looks as follows:

private static readonly (string, string)[] test = {("foo", "bar"), ("baz", "foz")};

There is no need to add specific type for ValueTuple. Notice that for .NET Core the NuGet package System.ValueTuple has to be installed.

over 4 years ago · Santiago Trujillo Relatório

0

Three short examples of using C# 7 syntax of array ValueTuple declaration & initialization:

(int Alpha, string Beta)[] array1 = { (Alpha: 43, Beta: "world"), (99, "foo") };
(int Alpha, string Beta)[] array2 = { (43, "world"), (99, "foo") };
(int, string)[] array3 = { (43, "world"), (99, "foo") };   // Item1, Item2, ...

These all compile fine on VS2017

Linq can be a good way to create arrays of ValueTuples, also:

var foo = Enumerable.Range(1,10).Select(z=> (Alpha: 43, Beta: "world")).ToArray();
over 4 years ago · Santiago Trujillo Relatório

0

You can create a new tuple like this

var logos = new (string Name, string Uri)[]
{
    ("White", "white.jpg"),
    ("Black", "black.jpg"),
};
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