简体   繁体   English

在 Visual Studio 中,我的 Winforms 程序不支持接口。 我怎样才能让它支持他们?

[英]In Visual Studio, my Winforms program is not supporting interfaces. How can I make it support them?

I've used Visual Studio in the past eg Visual Studio 2015, and prior, and not run into a problem like this.我过去使用过 Visual Studio,例如 Visual Studio 2015 和之前的版本,但没有遇到这样的问题。

I have Visual Studio 2019, and created a Winforms program and get this error我有 Visual Studio 2019,并创建了一个 Winforms 程序并收到此错误

Default interface implementation is not available in c# 7.3.默认接口实现在 c# 7.3 中不可用。 Please use language version 8.0 or higher请使用 8.0 或更高版本的语言

在此处输入图像描述

In the past, I used C# when it was c# 5.0 or earlier like C# v3 or C# v4, and I never ran into this.过去,我在 c# 5.0 或更早版本时使用 C#,例如 C# v3 或 ZD7EFA19,027D39D374Z5ADB4 和 IBE7D39D374Z5ADB4 从未遇到过。

I notice that Visual Studio 2019 offers two types of Winforms programs, .NET Framework and .NET Core我注意到 Visual Studio 2019 提供了两种类型的 Winforms 程序,.NET Framework 和 .NET Core

在此处输入图像描述

That error happens with the .NET Framework ones.该错误发生在 .NET 框架中。

When I create a Winforms project picking .NET Core, then I don't get that error.当我创建一个选择 .NET 核心的 Winforms 项目时,我没有收到该错误。 I don't really know what the difference is between these two.我真的不知道这两者之间有什么区别。 But why does a Winforms program picked with .NET framework not support interfaces, but with .NET Core would?但是为什么使用 .NET 框架选择的 Winforms 程序不支持接口,但使用 .NET Core 会?

I found one webpage said I need to install .net core 3, I did and the program (which was based on .NET framework) still didn't work.我发现一个网页说我需要安装 .net 核心 3,我安装了,但程序(基于 .NET 框架)仍然无法运行。

Am I not meant to be creating Winforms programs based on .NET Framework?我不是要创建基于 .NET 框架的 Winforms 程序吗? And only with .NET Core?并且只有 .NET 核心?

When I have a Winforms program based on .NET Framework, how am I supposed to get it to work?当我有一个基于 .NET 框架的 Winforms 程序时,我应该如何让它工作?

I looked at downloading C# 8 I see this link https://devblogs.microsoft.com/dotnet/take-c-8-0-for-a-spin/ mentions .NET Core 3, though I think I already installed .NET Core 3. And the problem is with Winforms programs based on the .NET Framework, rather than ones based on .NET Core I looked at downloading C# 8 I see this link https://devblogs.microsoft.com/dotnet/take-c-8-0-for-a-spin/ mentions .NET Core 3, though I think I already installed .NET Core 3 . 问题在于基于 .NET 框架的 Winforms 程序,而不是基于 .NET 核心的程序

You're trying to declare the following interface:您正在尝试声明以下接口:

interface abcd
{
    static int abcd;
}

You're using .NET Framework, which defaults to C# 7.3.您正在使用 .NET 框架,默认为 C# 7.3。 In C# 7.3, an interface cannot contain static members, or fields, so this is wrong twice.在 C# 7.3 中,接口不能包含 static 成员或字段,因此这是错误的两次。 Before default interface implementations were added in C# 8.0, this would have been a different error;在 C# 8.0 中添加默认接口实现之前,这将是一个不同的错误; probably one telling you that interfaces cannot contain fields.可能有人告诉您接口不能包含字段。 After C# 8.0, however, it is possible to do this sort of thing as part of a default implementation, so now the error assumes you were trying to do that, and tells you what version of C# you need to enable in order to do so.但是,在 C# 8.0 之后,可以将此类事情作为默认实现的一部分进行,因此现在错误假设您正在尝试这样做,并告诉您需要启用哪个版本的 C# 才能这样做.

If you weren't trying to add a default implementation, then you can fix your code by making that field into a property, and making that property not static.如果您不尝试添加默认实现,则可以通过将该字段设置为属性来修复您的代码,并使该属性不是 static。

interface abcd
{
    int abcd { get; set; }
}

Error is pretty clear that it's only allowed in C# 8.0 or greater.错误很明显,它只允许在 C# 8.0 或更高版本中使用。

And C# 8.0 supports .net core并且C# 8.0支持.net核心

.net frameworks don't support version greater then C# 7.3. .net 框架不支持高于 C# 7.3 的版本。

Reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#defaults参考: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#defaults

info from vivek, hans and anax helped..来自 vivek、hans 和 anax 的信息帮助了..

so there's two ways of creating a winforms application in VS 2019, one is .NET Core, one is .NET Framework (as we see looking at the image in the question)所以在VS 2019中有两种创建winforms应用程序的方法,一种是.NET Core,一种是.NET Framework(正如我们在问题中看到的图像)

Each is associated with a different version of the C# language.每个都与 C# 语言的不同版本相关联。

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

Target framework    version C#      language version  
.NET Core           3.x              C# 8.0
.NET Core           2.x              C# 7.3
.NET Standard       2.1              C# 8.0
.NET Standard       2.0              C# 7.3
.NET Standard       1.x              C# 7.3
.NET Framework      all              C# 7.3

.NET Framework is the old name for the framework / name for an older framework, and .NET Core is more recent. .NET Framework是框架的旧名称/旧框架的名称, .NET Core是更新的。

So it's probably better to create them choosing .NET Core因此,选择.NET Core创建它们可能会更好

And if creating a .NET Core one, then the language used is C# 8.如果创建一个 .NET Core one,那么使用的语言是 C# 8。

Whereas when creating it with .NET Framework, then the language used is C# 7.3而使用 .NET 框架创建它时,使用的语言是 C# 7.3

Both don't allow instance variables in an interface but the more recent one (c# 8), which is used with .NET Core, does allow static variables in an interface.两者都不允许接口中的实例变量,但与 .NET 核心一起使用的最新版本(c# 8)确实允许接口中的 static 变量。

在此处输入图像描述

在此处输入图像描述

VS 2015 used the old one, .NET Framework. VS 2015 使用旧的 .NET 框架。

In VS 2015, I must have just used getters and setters eg在 VS 2015 中,我必须使用 getter 和 setter,例如

int abcd { get; set; }

Which of course you can still do with the newer one, the .NET Core one.当然,您仍然可以使用较新的 .NET Core one。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让我的Visual Studio看起来像这样? - How can I make my Visual Studio look like this? 如何创建一个允许我输入LINQ表达式的控制台应用程序,我的程序将执行它们? - How can I make a console application that lets me enter LINQ expressions and my program will execute them? 如何制作 WinForms 冒泡排序程序? - How do I make a WinForms bubblesort program? 如何使WinForms应用程序具有DPI意识 - How can i Make my WinForms Application DPI-Aware 如何让Visual Studio使用我更新的NotifyIcon图标? - How can I make Visual Studio use my updated icon for my NotifyIcon? 我无法在Visual Studio的WinForms应用程序中单击Telerik控件 - I can't click on Telerik controls in my WinForms application in Visual Studio Visual Studio停止支持我的项目 - Visual Studio stopped supporting my projects 如何为Visual Studio制作我的应用程序的起始页面 - How I can make Start Page for My Application Like Visual studio 如何进行简单测试以验证Visual Studio 2015在设计模式下正在执行我的代码? - How can I make a simple test to verify that Visual Studio 2015 in Design Mode is executing my code? 如何使用NAnt引用在Visual Studio中生成的程序集(并使我的应用程序正常工作)? - How can I reference an assembly generated in Visual Studio with NAnt (and make my app work)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM