c# 使用未赋值的局部变量

标签 c#

嗨,这是我的代码,我想在列表中添加字符串,但出现此错误

use of unassigned local variable

这是我的代码

List<string> tokens = new List<string>();
int token_number=0 ;
char[] character = file_text.ToCharArray();
int i = 0;
for (; i < character.Length; i++)
{
    if (char.IsLetter(character[i]))
    {
        string local_token;
        while (char.IsLetterOrDigit(character[i + 1]) || character[i + 1] == '_')
        {

            local_token += character[i];
            tokens.Add(local_token.ToString());
        }

    }
}

嗨,这是我的代码,我想将字符串添加到我的列表中,所以得到这个错误 Error 2 Use of unassigned local variable 'local_token' C:\Users\Alireza\Documents\Visual Studio 2013\Projects\tslnc\tslnc\Program.cs 46 25 tslnc

最佳答案

将您的 local_token 声明更改为:

string local_token = "";

如果您没有为字符串指定值,则它不会被初始化(未分配)。

关于c# 使用未赋值的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825443/

相关文章:

c# - 如何在 .NET 中通过串行端口异步传输?

c# - 新的 .csproj 格式 - 如何将整个目录指定为子目录的 "linked file"?

c# - JSON 数组作为空值发送到 Controller

c# - LINQ 查询未提取所需的所有记录

c# - 如何为多对多关系编写简单的 LINQ 报告?

java - 从 Android 接收 Base64 编码的字符串到 C# 应用程序

c# - 无法在 MongoDB Web API 中按 ID 获取文档

c# - 自定义 ASP.NET MVC 路由服务 ".json" ".xml"Style Urls

c# - ASP.NET 中的 Crystal Report 问题 - ExportToHttpResponse

c# - 在 C# 中从串口接收数据有问题吗?