System.Argued tofrangeexception how to solve?

Asked

Viewed 135 times

1

Hello, I am getting an Exception when I run the code below stating that my List<> is out of range. I have tried everything, but without success. How do I solve?

private List<string> conexao;

    public Conexao()
    {
        conexao = new List<string>()
        {
            [0] = "127.0.0.1",
            [1] = "5432",
            [2] = "cstdb",
            [3] = "postgres",
            [4] = "#abc123#"
        };
    }

1 answer

2


If you’re declaring a List should not treat as if it were an array, stating the index ([0], etc.), the list works differently, treat this way:

var conexao = new List<string>()
{
    "127.0.0.1",
    "5432",
    "cstdb",
    "postgres",
    "#abc123#"
};

Then if you want you can use the index that will work: conexao[0]

  • Perfect, friend. Funny is that the Intelisense of Visual Studio that assembled it. But anyway thanks for the information. I’ll try here.

  • that bizarre, need to fire this Intellisense ai..:)

  • :) I’ll resign for just cause.

Browser other questions tagged

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