C # CS1526 compiler error: new expression requires (), [] or {} after type

I follow the tutorial on creating 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 get the indicated error on this line:

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

Does anyone know what I'm doing wrong?

+5
source share
4 answers

Using

Vehicle my_Car = new Vehicle();

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

One of the following is required:

  • ()to call the constructor. for example new Vehicle()ornew Vehicle(...)
  • {} as an initializer, for example. new Vehicle { year = 2010, mileage = 10000}
  • []for arrays, for example. new int[3], new int[]{1, 2, 3}Or evennew []{1, 2, 3}
+13

:

Vehicle my_Car = new Vehicle();
+4

try new Vehicle()

+2
source

Assuming you are working with C # 3 or later, you can also use implicit typing and do this:

var my_Car = new Vehicle();

The same IL is produced in both cases.

+1
source

All Articles