无法将带有 [] 的索引应用于“方法组"类型的表达式.不知道发生了什么 [英] Cannot apply indexing with [] to an expression of type 'method group'. No idea what is going on

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

问题描述

所以我试图读取字符串 theMap 中的 txt 文件,然后在 map 中复制 theMap 并返回 map.我也试图读取二维字符串中的 Txt 文件.然后返回的数组是映射.我想在控制台上打印它,这里有无法将 [] 索引应用到‘方法组’类型的表达式"问题也是代码.

So i'm trying to read a txt file in string theMap and then make a copy of theMap in map and return map. Also im trying to read the Txt file in 2d string. Then the returned array which is map. I want to print it at the console and there is the "Cannot apply indexing with [] to an expression of type 'method group'" problem here is the code also.

    public static string[,] Reader()
    {
        string theMap = System.IO.File.ReadAllText(@"C:\Users\Public\Console Slayer\Map\map.txt");


        int k = 0, l = 0;
        string[,] map = new string[11,54];
        foreach (var row in theMap.Split('\n'))
        {
            foreach (var col in row.Trim().Split(' '))
            {
                map[l,k] = (col.Trim());
                l++;
            }
            k++;
        }
        return map;
    }
    public static void Printer()
    {
        for (int y = 0; y < 11; y++)
        {
            for (int x = 0; x < 54; x++)
            {
                Console.Write(Reader[y,x]);
            }
            Console.WriteLine();
        }
    }
    static void Main()
    {
        Reader();
        Printer();
    }

推荐答案

Reader 是一种方法.你不能索引它,但你可以索引它的结果:

Reader is a method. You cannot index it, but you can index the result of it:

Console.Write(Reader()[y,x]);
//                  ^ You need these parens to invoke the method.

但是,这会为每个循环调用该函数,读取文件 11 * 54 = 594 次!读取文件一次并存储结果;无需在每次循环迭代时调用此方法:

However, this will invoke the function for every loop, reading the file in 11 * 54 = 594 times! Read the file once and store the result instead; there is no need to call this method on each loop iteration:

var data = Reader();

for (int y = 0; y < 11; y++)
{
    for (int x = 0; x < 54; x++)
    {
        Console.Write(data[y,x]);
    }
    Console.WriteLine();
}

这篇关于无法将带有 [] 的索引应用于“方法组"类型的表达式.不知道发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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