无法在C#中的类、结构或接口成员声明中使用无效的标记’=>’。

huangapple go评论132阅读模式
英文:

Invalid token '=>' in class, struct, or interface member declaration in c#

问题

我使用Visual Studio 2015。代码中有问题吗?请指导。我在C#方面还是个新手。

public partial class Root : Form
{
    private BindingList<Product> list = null;
    private BindingSource bindingSource = null;

    public Root() => InitializeComponent();
}
英文:

I'm using Visual Studio 2015. Is there a problem in the code? Please guide. I'm also still a beginner in C#.

public partial class Root : Form
{
    private BindingList&lt;Product&gt; list = null;
    private BindingSource bindingSource = null;

    public Root() =&gt; InitializeComponent();
}

答案1

得分: 2

你正在使用随附于Visual Studio 2015的旧版本C#。如果可以的话,你应该升级到Visual Studio 2022,以便可以使用语言特性,比如表达式体构造函数

如果你不能或不想升级到最新的Visual Studio和C#版本,那么简单的解决方法是将构造函数更改为这样:

public partial class Root : Form
{
    //...

    public Root()
    {
        InitializeComponent();
    }
}

正如Jon Skeet所指出的,表达式体构造函数仅支持C# 7.0及更高版本。

英文:

You are using an old version of C# that comes with Visual Studio 2015. If you can, you should upgrade to Visual Studio 2022, so that you can use language features like expression bodied constructors.

If you cannot or do not want to upgrade to the latest Visual Studio and C# versions, then the easy fix is to change the constructor like this:

public partial class Root : Form
{
    //...

    public Root()
    {
        InitializeComponent();
    }
}

As Jon Skeet noted, expression-bodied constructors are only supported in C# 7.0 and higher.

huangapple
  • 本文由 发表于 2023年7月6日 15:15:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定