0

I am now learning how to use the For Loops in C#, but when I try to compile my program, three errors in the console appear and one of them says that int doesn't have a definition for Lenght (I'm just not showing the errors because they are in portuguese), any idea about why this is happening?

using System;

namespace Giraffe
{
    class Program
    {
        static void Main(string[] args)
        {
            int luckyNumbers = {4, 8, 15, 16, 23, 42};

            for (int i = 0; i < luckyNumbers.Length; i++)
            {
                Console.WriteLine(luckyNumbers[i]);
            }

            Console.ReadLine();
        }

    }
}  
Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
AmanoSkullGZ
  • 141
  • 1
  • 1
  • 7
  • 1
    There is a syntax error in this line `int luckyNumbers = {4, 8, 15, 16, 23, 42};`. The proper way to declare and initialize an array is `int[] luckyNumbers = new [] { 4, 8, 15, 16, 23, 42 };` ([doc](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays#array-initialization)) – colinD May 17 '20 at 21:01

1 Answers1

3

You have an incorrect array declaration, change it to

int[] luckyNumbers = {4, 8, 15, 16, 23, 42};

You current declaration int luckyNumbers = {4, 8, 15, 16, 23, 42}; is invalid, you can't assign an array instance to int variable, therefore Length property isn't available

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66