c# - 接口(interface)有什么方法可以导致不同的行为吗?

标签 c#

假设我有以下代码:

class Foo: IFoo {
    public string fooProp { get; set; }
}

interface IFoo {
    string fooProp {get; set; }
}

是否有可能在以下情况之间存在不同的行为:

Foo x = new Foo();
someMethod(x);

和:

IFoo x = new Foo();
someMethod(x);

?

最佳答案

我认为可能有所不同。如果有人使用了糟糕的编程风格,即:

public void someMethod(IFoo f)
{
    if (f is Foo)
    {
        Foo f1 = (Foo)f;
        //Do smth with unique Foo members
    }
    //Do anything with IFoo members
}

关于c# - 接口(interface)有什么方法可以导致不同的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13731889/

相关文章:

c# - 是否可以覆盖 HostAuthenticationFilter 过滤器?

c# - 从坐标获得第一、第二、第三邻居的算法

c# - 通过 TCP 读取嗅探数据

c# - 调用 tojson() 后创建 3 个 List<column>

c# - 如何从小数部分取零?

c# - MVC4 .NET 4.5 异步操作方法不重定向

c# - Autofac基于TypedParameter的不同服务解析

c# - 当构造函数有参数时使用 Moq 实现模拟对象

javascript - 时间:2019-03-17 标签:c#xorcryptotojavascriptdecrypt

c# - 如何获得磁盘/文件上图像的宽 x 高比?