在C#中声明一个方法中的变量或数组 [英] Declare a variable or array inside a methode in C#

查看:259
本文介绍了在C#中声明一个方法中的变量或数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在c#中使用数组来进行学习。但是当我在一个方法中声明我的数组时,它会给出错误}。这是我的代码

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 kwaterminal
{
class RandomTerain
{
public static void generate()
{
public int [,] terain = new int [ 16 16 ];

}
}
}





我尝试过:



i heave在网上搜索但我找不到一个好答案

解决方案

< a href =https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/public> public [ ^ ]是类型和类型成员的访问修饰符 - 不适用于在方法内声明的变量。删除 public 关键字,它将编译正常,并且 terain 将可以在方法内的任何位置访问(但不能在法)。

so i wanted to play around with arrays in c# for learning purpose. but when i declare my array inside a method it gives the error } expected. here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace kwaterminal
{
    class RandomTerain
    {
        public static void generate()
        {
            public int[,] terain = new int[16, 16];

        }
    }
}



What I have tried:

i heave searched the web but i could not find a good answer

解决方案

public[^] is an access modifier for types and type members - not for variables declared inside a method. Remove the public keyword and it will compile fine, and terain will be accessible anywhere inside the method (but not outside the method).


这篇关于在C#中声明一个方法中的变量或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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