Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Main方法c中设置全局变量#_C#_.net - Fatal编程技术网

C# 如何在Main方法c中设置全局变量#

C# 如何在Main方法c中设置全局变量#,c#,.net,C#,.net,我在foreach中有两个用户名和密码变量,我想设置为全局变量,但我不知道如何设置? 我尝试使用此代码,但当我尝试使用此变量替换用户名和密码字符串时,我收到消息: Use of unsigned local variable 'username'. Use of unsigned local variable 'password'. 守则: public static void Main() { string[] lineOfCo

我在
foreach
中有两个用户名和密码变量,我想设置为全局变量,但我不知道如何设置? 我尝试使用此代码,但当我尝试使用此变量替换用户名和密码字符串时,我收到消息:

Use of unsigned local variable 'username'.
Use of unsigned local variable 'password'.
守则:

            public static void Main()
        {
            string[] lineOfContents = File.ReadAllLines(@"C:\\M\send.txt");

            string username;
            string password;

            foreach (var line in lineOfContents)
            {
                string[] tokens = line.Split(',');
                string user = tokens[0];
                string pass = tokens[1];
                username = user;
                password = pass;
            }

            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://199.199.199.199/Plovdiv.txt");
                request.Method = WebRequestMethods.Ftp.UploadFile;

                // This example assumes the FTP site uses anonymous logon.  
                request.Credentials = new NetworkCredential(username, password);

编译器不知道循环是否会执行,这就是为什么它会显示这样的警告,即循环可能会执行,也可能不会执行,如果它跳过迭代(
lineOfContents
为null或没有值),那么您的局部变量
username
password
将不会初始化,并导致异常,为了避免必须使用一些默认值初始化它们,因此声明如下所示:

string username = ""; // String.Empty or some default values
string password = "";
// rest of code here

编译器不知道循环是否会执行,这就是为什么它会显示这样的警告,即循环可能会执行,也可能不会执行,如果它跳过迭代(
lineOfContents
为null或没有值),那么您的局部变量
username
password
将不会初始化,并导致异常,为了避免必须使用一些默认值初始化它们,因此声明如下所示:

string username = ""; // String.Empty or some default values
string password = "";
// rest of code here

foreach
循环中为
username
password
赋值,但是编译器不知道
lineOfContents
是否为空,以及何时到达
request.Credentials=newnetworkcredential(username,password)变量将具有任何值。您需要初始化它们

string username = string.Empty;
string password = string.Empty;

foreach
循环中为
username
password
赋值,但是编译器不知道
lineOfContents
是否为空,以及何时到达
request.Credentials=newnetworkcredential(username,password)变量将具有任何值。您需要初始化它们

string username = string.Empty;
string password = string.Empty;

您当前的代码逻辑错误:

  • 如果
    C:\M\send.txt
    文件为空:
    foreach
    根本不循环,并且
    username
    密码将包含垃圾,该怎么办
  • 为什么要在整个文件上循环?它是文件的最后一行,最终将分配给
    用户名
    密码
  • 如果要保留现有逻辑(解析并分配文件的最后一行):


    您当前的代码逻辑错误:

  • 如果
    C:\M\send.txt
    文件为空:
    foreach
    根本不循环,并且
    username
    密码将包含垃圾,该怎么办
  • 为什么要在整个文件上循环?它是文件的最后一行,最终将分配给
    用户名
    密码
  • 如果要保留现有逻辑(解析并分配文件的最后一行):


    这是因为您初始化了变量,但没有为它们设置任何值。您应该为它们指定一个初始值。例如,
    string username=“”;字符串密码=”这是因为您初始化了变量,但没有为它们设置任何值。您应该为它们指定一个初始值。例如,
    string username=“”;字符串密码=”谢谢各位..谢谢各位。。