public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
bool hasJumped = true;
Vector2 velocity;
Texture2D player;
Texture2D ground1;
List<Vector2> vectors = new List<Vector2>();
List<int> list = new List<int>();
List.add(1);
List.add(1);
导致 2 个错误"Invalid Token '(' in class,struct,or interface member declaration"
和"using the generetic type 'System.Collections.Generic.List<T>' requiers 1type arguments"
这是怎么回事请告诉我
正确的情况是list.Add(1)
你应该使用 list.Add(1)
而不是 List.add(1)
。实例的名称list
而不是List
,方法的名称Add
而不是add
。此外,您不能在类的主体中进行方法调用,而是在类中某个方法的主体中进行方法调用。
你不能在类的主体中有这个:
List<int> list = new List<int>();
list.Add(1);
但是你可以在身体中创建一个List
,并有一个这样的方法:
List<int> list = new List<int>();
public void AddOne()
{
list.Add(1);
}
或者,您可以在正文中声明一个list
,然后在方法中实例化它并调用Add
如下所示:
List<int> list;
public void CreateListAndAddOne()
{
list = new List<int>();
list.Add(1);
}
而不是List.add(1)
使用list.Add(1);
。
编辑:
另一件事你不能在你的类中以这种方式使用它,但你需要在方法、构造函数或属性中使用它。但是,解决方案可能是:
List<int> list = new List<int>(){ 1 };
Add
不是静态方法,它是一个实例方法。
重命名变量将有助于缓解混淆(请记住,C# 区分大小写):
List<int> myIntegerList = new List<int>();
myIntegerList.Add(1);