c#编译器错误CS1526:类型后需要一个新表达式requires(),[]或{} [英] c# compiler error CS1526: A new expression requires (), [], or {} after type

查看:511
本文介绍了c#编译器错误CS1526:类型后需要一个新表达式requires(),[]或{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的教程创建一个类:

I am following a tutorial to create a class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Session3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Vehicle my_Car = new Vehicle;
        }
    }
    class Vehicle
    {
        uint mileage;
        byte year;
    }
}



我收到提到的错误在这行:

I am getting the mentioned error on this line:

private void button1_Click(object sender, EventArgs e)
{
    Vehicle my_Car = new Vehicle;
}



有谁知道我做错了?

Does anyone know what I am doing wrong?

推荐答案

使用

Vehicle my_Car = new Vehicle();

要叫你需要一个构造函数() 。在类名后,就像函数调用

To call a constructor you need () after the class name, just like for function calls.

一个要求如下的:


  • ()的构造函数调用。例如新车()新车(...)

  • {} 作为初始化,例如新车{一年= 2010,里程数= 10000}

  • [] 对于数组,如新INT [3] 新INT [] {1,2,3} 甚至只是新[] {1,2,3}

  • () for a constructor call. e.g. new Vehicle() or new Vehicle(...)
  • {} as an initializer, e.g. new Vehicle { year = 2010, mileage = 10000}
  • [] for arrays, e.g. new int[3], new int[]{1, 2, 3} or even just new []{1, 2, 3}

这篇关于c#编译器错误CS1526:类型后需要一个新表达式requires(),[]或{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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