Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Encapsulation and Arrays Arrays

Help with this code for arrays

So the question asked me to declare a double named laTimes and give it 4. I think thats what I did but it keeps telling me I'm wrong can someone see what my mistake is?

CodeChallenge.cs
double [] lapTimes = new double [4];
Scott Wyngarden
Scott Wyngarden
Courses Plus Student 16,700 Points

your syntax is just a little off, just remove the space between double and [].

double[] lapTimes = new double[4];
Trey Perry
Trey Perry
3,346 Points

Don't forget you can also add individual values to your array when declaring. The compiler is smart enough to create the array with the needed size from the values passed in. The following would create an array of doubles with size 4, with the entered values in indices 0-3.

double[] lapTimes = new double[] {2.14, 3.1, 2.56, 1.99};

To get the length you could do:

int Length = lapTimes.Length;

The value of Length would be 4.

Steven Parker
Steven Parker
231,198 Points

I don't think you can initialize that way, but you can with slightly different punctuation:

double[] lapTimes = new double[] {2.14, 3.1, 2.56, 1.99};

NOTE: This is for general understanding, I am not suggesting this for use in the challenge.

Trey Perry
Trey Perry
3,346 Points

Oops! Thanks for catching my error Steven!

1 Answer

Steven Parker
Steven Parker
231,198 Points

The challenges can sometimes be pickier than the compiler. While extra spaces wouldn't be a problem for the compiler, as Scott pointed out, the challenge considers them incorrect.

You can also simplify the declaration a bit using the "var" keyword:

var lapTimes = new double[4];