Search code examples
c#arraysienumerableenumerable

C# Get accessor is inaccessible


I have the following class definition:

 public static string SplitString(string someText)
 {
      var queryArray = Regex.Split(someText, "\\s+(?=\\w+)");
      foreach (var i in Enumerable.Range(0, queryArray.Count - 1)) {
           // Some code
      }
 }

The problem is that queryArray.Count is giving me the following error:

The property 'System.Array.Count' cannot be used in this context because the get accessor is inaccessable.

What am i missing here?


Solution

  • You may try the Length property instead:

    public static string SplitString(string someText)
    {
        var queryArray = Regex.Split(someText, "\\s+(?=\\w+)");
        foreach (var i in Enumerable.Range(0, queryArray.Length - 1)) {
            // Some code
        }
    }
    

    Also your code would probably have been more readable if it was written like this:

    public static string SplitString(string someText)
    {
        var queryArray = Regex.Split(someText, "\\s+(?=\\w+)");
        for (var i = 0; i < queryArray.Length; i++) {
            // Some code
        }
    }
    

    or like this:

    public static string SplitString(string someText)
    {
        var queryArray = Regex.Split(someText, "\\s+(?=\\w+)");
        foreach (var item in queryArray) {
            // Some code
        }
    }