Search code examples
c#arrayscompiler-errorsstring-length

Why does my C# program not recognize length?


I have a very simple program directly from my textbook that is meant to print a list of words names that you type to the command line.

When I try to run the code, I get an error that states...

"error CS1061: 'string[]' does not contain a definition for 'length' and no accessible extension method 'length' accepting a first argument of type 'string[]' could be found (are you missing a using directive or an assembly reference?)"

Note: I am not running this through Microsoft Visual Studio because the assignment involves running code through a text editor.

Code here:

// 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);
    }
}

A link to the program assignment here.

(Scroll halfway down the webpage, assignment starts after the title "HelloWorld2 - Using Dev Prompt".)

Any help is greatly appreciated!


Solution

  • Length is a property. Properties are written in pascal case. Looks like you only have a typo in your second Console.WriteLine. There is an args.length with a lowercase l.