为什么C#是不允许非成员函数像C ++ [英] Why C# is not allowing non-member functions like C++

查看:223
本文介绍了为什么C#是不允许非成员函数像C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#不允许写非成员函数和每个方法应该是类的一部分。我想这是所有CLI语言的限制。可是我错了,我发现,C ++ / CLI支持非成员函数。当它被编译,编译器将使方法,一些不愿透露姓名的类的成员。

C# will not allow to write non-member functions and every method should be part of a class. I was thinking this as a restriction in all CLI languages. But I was wrong and I found that C++/CLI supports non-member functions. When it is compiled, compiler will make the method as member of some unnamed class.

下面是C ++ / CLI标准说,

Here is what C++/CLI standard says,

[注:非成员函数通过CLI视为一些不知名的类的成员;然而,在C ++ / CLI源$ C ​​$ C,这样的功能不能明确与类名合格。注完]

[Note: Non-member functions are treated by the CLI as members of some unnamed class; however, in C++/CLI source code, such functions cannot be qualified explicitly with that class name. end note]

非成员函数中的元数据编码是不确定的。 [注:这不会导致互操作性问题,因为这样的功能不能有公众知名度。注完]

The encoding of non-member functions in metadata is unspecified. [Note: This does not cause interop problems because such functions cannot have public visibility. end note]

所以我的问题是,为什么不C#实现这样的事情?或者你觉得不应该有非成员函数和每个方法应该属于某个类?

So my question is why don't C# implement something like this? Or do you think there should not be non-member functions and every method should belong to some class?

我的观点是有非成员函数的支持,并有助于避免污染类的接口。

My opinion is to have non-member function support and it helps to avoid polluting class's interface.

任何想法..?

推荐答案

请参阅此博客文章:

<一个href="http://blogs.msdn.com/ericlippert/archive/2009/06/22/why-doesn-t-c-implement-top-level-methods.aspx">http://blogs.msdn.com/ericlippert/archive/2009/06/22/why-doesn-t-c-implement-top-level-methods.aspx

和这个后续发布:

<一个href="http://blogs.msdn.com/ericlippert/archive/2009/06/24/it-already-is-a-scripting-language.aspx">http://blogs.msdn.com/ericlippert/archive/2009/06/24/it-already-is-a-scripting-language.aspx

这篇关于为什么C#是不允许非成员函数像C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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