C# is not finding/recognizing my List

Asked

Viewed 48 times

0

I have a List made with the following code:

List<frase> frases = new List<frase>();

According to the documentation of System.Collections.Generic, my code is right. But for some reason C# is not finding the List, what I’m doing wrong?

inserir a descrição da imagem aqui

  • You are trying to use the variable in the class scope, not within a method. So it won’t work.

  • @Gabrielkatakura It worked, thanks. Wouldn’t it be better to write this in answer form? To make it more organized and also to make it easier for those who have the same problem in the future.

  • Okay, I made an answer indicating the solution.

1 answer

2


You are trying to use the variable in the class scope, not within a method. So it won’t work.

public class User
{
  List<Frase> frases = new List<Frase>(); // isso é um atributo, não uma variável local

  // isso abaixo não irá funcionar, não é possível executar
  // trechos de código neste estilo no escopo da classe
  frases.Add(new Frase("Olá"));

  public void Metodo()
  {
    frases.Add(new Frase("Olá")); // aqui o atributo está sendo usado corretamente, não terá problemas
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.