相关疑难解决方法(0)

如何使接口的实现产生auto属性而不是NotImplementedException?

考虑到我们有一个简单的界面,例如ICar当我将鼠标移到ICar表达式上并单击Implement InterfaceVisual Studio生成下面的实现时.

有没有办法在界面上提供一个自动属性.这导致重新分解问题,每次都让我发疯!

    public interface ICar
    {
        double Power { get; set; }
    }

    public class Car:ICar
    {

        public double Power
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# visual-studio-2010 visual-studio

27
推荐指数
2
解决办法
3644
查看次数

实现接口包括抛出新的NotImplementedException ...为什么?

我正在使用VS2017社区,它昨天刚收到更新.今天我想实现一个接口,现在实现如下:

public string City 
{ 
    get => throw new NotImplementedException(); 
    set => throw new NotImplementedException(); 
}
Run Code Online (Sandbox Code Playgroud)

而不是这(我的预期):

public string City { get; set; }
Run Code Online (Sandbox Code Playgroud)

为何如此改变?不确定这是否特定于C#7或VS或其他什么.我只知道接口的自动实现在过去一周左右发生了变化.

我的界面:

public interface IMyInterface
{
    string City { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

c# visual-studio-2017

27
推荐指数
3
解决办法
4503
查看次数

在将接口实现到类中时如何创建自动实现的属性?

当我第一次在一个类中实现一个接口时,我希望resharper 6或visual studio 2010将我的属性实现为自动实现的属性,而不是put new nonImplementedException();的默认值.我怎样才能做到这一点?例如:

public interface IEmployee
{
// want this to stay just like this when  implemented into class
ID { get; set; }
}

public class Employee : IEmployee
{
// I do not want the throw new NonImplemented exception
// I want it to just appear as an auto implemented property
// as I defined it in the interface
public int ID
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            } …
Run Code Online (Sandbox Code Playgroud)

c# properties interface class

14
推荐指数
2
解决办法
8096
查看次数