无法将[]的索引应用于“数组”类型的表达式 [英] Cannot apply indexing with [] to an expression of type 'Array''

查看:170
本文介绍了无法将[]的索引应用于“数组”类型的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下代码,我得到了无法对[Array]类型的表达式应用[]进行索引编制。目的是创建一个计算器模板,然后调用这些方法来运行各种操作。
标有// HERE的区域中出现错误。.请帮助。我是涉及C#编码的新手,因此,感谢所有帮助,如果有人也可以向我解释这个问题,我希望获得帮助。谢谢

I am getting the "Cannot apply indexing with [] to an expression of type 'Array''" for the below code.. Aim is to create a calculator template and then call these methods to run the various operations. Errors are coming in the areas marked //HERE.. Please help. I am a newbie when it comes to c# coding, so, all help is appreciated and I would like it if someone could explain me the issue too. Thanks

private static Array NumberFeedLengthDecider()
{
    Console.WriteLine("Please enter how many numbers that you would like to add.");
    int i = Convert.ToInt32(Console.ReadLine());
    int[] numbers = new int[i];

    return numbers;
}
private static int NumberFeed(Array numbers)
{
    Console.WriteLine("Please enter the numbers one by one, each followed by the 'Enter' key.");
    int i = numbers.Length;

    for (int counter = 0; counter < i; counter++)
    {
        int temp = Convert.ToInt32(Console.ReadLine());
        numbers[counter] = temp; //HERE
    }
    return i;
}
private static void NumberDisplay(Array numbers)
{
    Console.WriteLine("The numbers you have entered are: ");
    int i = (numbers.Length);

    for (int x = 0; x < i; x++)
    {
        Console.WriteLine(numbers[x]); //HERE
    }
}

基本上,我想创建一个方法确定要执行操作的数字数,第一个是数字(numberFeedLengthDecider),然后是将数字馈入该数组的另一种方法(NumberFeed),然后是显示该数字组的另一种方法(NumberDisplay)。但是由于某种原因,我似乎无法使其正常工作

Basically, I want to create a method for deciding the number of numbers the operations are to be run on which is the first one (numberFeedLengthDecider), then another method to feed the numbers into that array (NumberFeed), and then another method to display that group of numbers (NumberDisplay). but for some reason, I can't seem to get it to work

推荐答案

Array 是数组的基类,它的元素不是强类型的;您可以在其中放置任何对象。

Array is the base class for arrays, its elements aren't "strongly typed"; you can put any object in it.

由于您似乎只在处理 int 元素,因此您应该使用 int [] ,现在使用 Array 。然后,可以使用[]索引访问元素,并确保每个元素都是要引导的 int

Since you seem to be dealing with int elements only, you should be using int[] where you now use Array. You can then access elements with the [] indexing, and you ensure that each element is an int to boot.

这篇关于无法将[]的索引应用于“数组”类型的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆