【问题标题】:Why does my C# program not recognize length?为什么我的 C# 程序不能识别长度?
【发布时间】:2025-12-13 00:20:04
【问题描述】:

我有一个非常简单的程序,直接来自我的教科书,用于打印您在命令行中键入的单词名称列表。

当我尝试运行代码时,我收到一条错误消息,指出...

"错误 CS1061: 'string[]' 不包含 'length' 的定义,并且找不到可访问的扩展方法 'length' 接受类型为 'string[]' 的第一个参数(您是否缺少 using 指令还是程序集参考?)”

注意:我没有通过 Microsoft Visual Studio 运行它,因为作业涉及通过文本编辑器运行代码。

代码在这里:

// Hello World2.cs
using System;

public class HelloWorld2
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Hello, World.");
        Console.WriteLine("You entered the following {0} names " +
            "on the command line:", args.length );
        for (int i=0; i < args.Length; i++)
        {
            Console.WriteLine("{0}", args[i]);
        }
            Console.ReadKey(true);
    }
}

指向程序分配here的链接。

(向下滚动网页一半,作业在标题“HelloWorld2 - 使用开发提示”之后开始。)

非常感谢任何帮助!

【问题讨论】:

  • length != Length.

标签: c# arrays compiler-errors string-length


【解决方案1】:

Length 是一个属性。属性以帕斯卡大小写。 看起来您的第二个 Console.WriteLine 中只有一个错字。有一个 args。length 带有小写 l。

【讨论】:

  • 你说得对,我不敢相信我忽略了这么简单的错字.. 非常感谢!我已经更新了代码以修复错字并且它有效!
【解决方案2】:

Console.WriteLine("您输入了以下 {0} 个名称" + "在命令行上:", args.Length );它应该是长度

【讨论】: