非虚拟接口(NVI)成语在C#中与C ++中有用吗? [英] Is the Non-Virtual Interface (NVI) idiom as useful in C# as in C++?

查看:155
本文介绍了非虚拟接口(NVI)成语在C#中与C ++中有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,我经常需要 NVI 获取我的API的一致性不过C#中我并没有看到它使用的很多。我想知道是不是因为C#,作为一种语言,提供使NVI不必要的功能? (我仍然在C#中使用NVI,虽然在需要的地方)。

In C++, I often needed NVI to get consistency in my APIs. I don't see it used as much among others in C#, though. I wonder if that is because C#, as a language, offers features that makes NVI unnecessary? (I still use NVI in C#, though, where needed.)

推荐答案

我认为解释只是在C#传统的Java风格的OOP更根深蒂固,NVI与此相反。 C#有一个真正的接口类型,而NVI依赖于接口实际上是一个基类。这是C ++的完成,所以它自然适合。

I think the explanation is simply that in C#, "traditional" Java-style OOP is much more ingrained, and NVI runs counter to that. C# has a real interface type, whereas NVI relies on the "interface" actually being a base class. That's how it's done in C++ anyway, so it fits naturally there.

在C#中,它仍然可以完成,它仍然是一个非常有用的成语(更是如此,我会说,而不是正常界面),但它需要忽略内置的语言功能。

In C#, it can still be done, and it is still a very useful idiom (far more so, I'd say, than "normal" interfaces), but it requires you to ignore a built-in language feature.

许多C#程序员根本不会想到NVI类作为适当的接口。我认为这种精神抵触是C#中不常见的唯一原因。

Many C# programmers just wouldn't think of a NVI class as being "a proper interface". I think this mental resistance is the only reason why it's less common in C#.

这篇关于非虚拟接口(NVI)成语在C#中与C ++中有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆