stackman
stackman

Reputation: 342

Cannot iterate through array in xamarin forms (C#)

I have a string array and I am trying to iterate though it using foreach, however it doesn't even recognize the variable I used for the array.

Here is the code I have:

public class test
{
    string[] letters = new string[5] { "a", "b", "c", "d", "e" };

    foreach (string i in letters)
        Debug.WriteLine(i);
} 

It says the name 'lower' does not exist in the current context

Upvotes: 1

Views: 230

Answers (1)

Christos
Christos

Reputation: 53958

You have defined a class, but you haven't defined a method. The code you refer to should be defined in a method. You should get a warning for this by visual studio if this is your editor.

public class Test
{
    public static void YourMethodName()
    {
        var letters = new [] { "a", "b", "c", "d", "e" };

        foreach (var letter in letters)
        {
            Debug.WriteLine(letter);
        }
    }
} 

Then by calling this as below:

Test.YourMethodName();

You will get the expected output.

Upvotes: 2

Related Questions