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 Loops and Final Touches For Loops

Daniel Glennie
Daniel Glennie
42,854 Points

Property should return average value in an array but won't compile?

I'm getting a CS0019 compiler error with this code. It seems as if I can't add the array elements to the sum because it doesn't think they are of type int. Do I need to call the get accessor in the Frog Class to retrieve these values?

FrogStats.cs
namespace Treehouse.CodeChallenges
{
    class FrogStats
    {
        public static double GetAverageTongueLength(Frog[] frogs)
        {
            int sum = 0;
            double avg;
            for (int i = 0; i < frogs.Length; i++)
            {
                sum += frogs[i];
            }
            avg = (double) sum / frogs.Length;
            return avg; 
        }
    }
}
Frog.cs
namespace Treehouse.CodeChallenges
{
    public class Frog
    {
        public int TongueLength { get; }

        public Frog(int tongueLength)
        {
            TongueLength = tongueLength;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,268 Points

:point_right: The type of "frogs[i]" is Frog.

On the other hand, the type of "frogs[i].TongueLength" would be int. :wink: