0

Hi everyone,

I'm getting the following error:
Cannot apply indexing with [] to an expression of type 'System.Array'

I'm trying to print out the first element of the array as a string. Can someone explain what the error message means?

using System;
using System.Collections.Generic;

public class MyClass
{
    public static void RunSnippet()
    {
        string s = "12 23 34 45 56 67 34 45 56";
        Array a = s.Split(' ');
        Console.WriteLine(a[0]);
        Console.ReadKey();
    }   
}
3
Contributors
2
Replies
12
Views
4 Years
Discussion Span
Last Post by __avd
1

The string Split method returns an array of strings, so you cannot assign it to Array a. Use:
string[] a = s.Split(' '); Now a is an array of strings.
Side remark: Give meaningfull names to your variables, don't call them a,s et.

Votes + Comments
Hey, using string[] worked. And variable names: noted. TY.
1

The System.Array is base/super class of array. Here you need to request a method : a.GetValue(0)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.