错误:使用未分配的局部变量(字符串数组)数组、字符串、变量、局部

2023-09-03 17:20:41 作者:风灵无畏

我从我的App.config文件中读取连接字符串,并为我有以下code。

I am reading connection strings from my App.config file and for that i have following code.

try
 {
    string[] dbnames;
    int counter = 0;
    foreach (ConnectionStringSettings connSettings in ConfigurationManager.ConnectionStrings) 
    {
        dbnames[counter] = connSettings.Name;
        counter++;
    }
    return dbnames;
 }
 catch
 {
    throw;
 }

ç给我的错误使用未分配的局部变量的数据库名称此$ C $。我将有多个连接字符串在我的App.config。它们可以是无,1,2等。根据不同的需要。所以我不能静态分配的DBNAME大小。因为可以有一个情况下,如果它们超过分配大小的值。例如。如果我给它分配一个大小为5,而如果我获得第6的连接字符串。如果我有1,那么剩下的4会造成内存的浪费。

this code giving me error use of unassigned local variable for dbnames. i will have multiple connection strings in my App.config. They can be none,1,2 and so on. Depending on the needs. so i cant statically assign the dbname size. Because there can be a scenario if they exceed the value of assigned size. eg. if i assign it a size of 5, and what if i get 6th connection string. and if i have 1, then remaining 4 will be a memory wastage.

如果我错了,然后让我知道。

If i am wrong then let me know.

感谢。

推荐答案

使用此同时初始化数组。

Use this while initializing the array.

 string[] dbnames = new string[ConfigurationManager.ConnectionStrings.Count];

或使用名单,其中,串>

 
精彩推荐