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();
    }   
}

Recommended Answers

All 2 Replies

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.

commented: Hey, using string[] worked. And variable names: noted. TY. +0

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.