简体   繁体   中英

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.

I have Visual Studio 2019, and created a Winforms program and get this error

Default interface implementation is not available in c# 7.3. Please use language version 8.0 or higher

在此处输入图像描述

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.

I notice that Visual Studio 2019 offers two types of Winforms programs, .NET Framework and .NET Core

在此处输入图像描述

That error happens with the .NET Framework ones.

When I create a Winforms project picking .NET Core, then I don't get that error. 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?

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.

Am I not meant to be creating Winforms programs based on .NET Framework? And only with .NET Core?

When I have a Winforms program based on .NET Framework, how am I supposed to get it to work?

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

You're trying to declare the following interface:

interface abcd
{
    static int abcd;
}

You're using .NET Framework, which defaults to C# 7.3. In C# 7.3, an interface cannot contain static members, or fields, so this is wrong twice. Before default interface implementations were added in C# 8.0, this would have been a different error; 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.

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.

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

Error is pretty clear that it's only allowed in C# 8.0 or greater.

And C# 8.0 supports .net core

.net frameworks don't support version greater then C# 7.3.

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

info from vivek, hans and anax helped..

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)

Each is associated with a different version of the C# language.

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.

So it's probably better to create them choosing .NET Core

And if creating a .NET Core one, then the language used is C# 8.

Whereas when creating it with .NET Framework, then the language used is 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.

在此处输入图像描述

在此处输入图像描述

VS 2015 used the old one, .NET Framework.

In VS 2015, I must have just used getters and setters eg

int abcd { get; set; }

Which of course you can still do with the newer one, the .NET Core one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM