我可以在 C# 中包含一个静态类,这样我就不必键入类名来使用其静态函数吗?
在 VB.NET 中,我有一个名为 ArrayExtensions 的静态类,其中包含诸如 Join(Of T)(ParamArray arrays As T()()) As T()
之类的静态函数。他们在一个模块中。我不需要包含该模块;我可以从项目中的任何其他类中输入 Join(array1, array2, array3)
。
现在我正在尝试 C#,我在静态类 ArrayExtensions
中有相同的静态函数 T[] Join
。我似乎无法弄清楚如何避免每次都必须编写 ArrayExtensions.Join(array1, array2, array3) 。我尝试使用 ArrayExtensions ,但与 VB.NET 不同,它不起作用。
有什么方法可以在 C# 中输入 Join(array1, array2, array3)
吗?
我曾考虑将其更改为 T[] Join
并使用 {array1, array2}.Join()
,结果发现与 VB.NET 不同,C# 强制我编写 new [] {array1, array2}.Join()
。编译器无法判断花括号中是否包含语句或数组项?令人失望。
In VB.NET I had a static class called ArrayExtensions which contained static functions like Join(Of T)(ParamArray arrays As T()()) As T()
. They were in a Module. I didn't need to include the module; I could just type Join(array1, array2, array3)
from any other class in the project.
Now that I'm trying out C#, I have the same static function T[] Join<T>(params T[][] arrays)
in a static class ArrayExtensions
. I can't seem to figure out how to escape having to write ArrayExtensions.Join(array1, array2, array3)
every time. I tried using ArrayExtensions
which did not work, unlike VB.NET.
Is there any way I can just type Join(array1, array2, array3)
in C#?
I debated changing it to T[] Join<T>(this T[][] arrays)
and using {array1, array2}.Join()
, only to discover that unlike VB.NET C# forces me to write new [] {array1, array2}.Join()
. Couldn't the compiler figure out whether braces contained statements or array items? Disappointing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,没有。这是一个相当频繁请求的功能。如果能够做类似的事情那就太好了:
而不是
这是“很好有但完全不必要”功能的完美示例。它从来没有足够引人注目,以至于无法真正将其排在需要实施的清单上足够高的位置。我们有一个“很好但完全没有必要”的功能列表,这些功能实际上比您的手臂还长,并且我们无法在任何版本中实现超过几个功能,抱歉!
Unfortunately, no. This is a fairly frequently requested feature. It would be nice to be able to do something like:
instead of
This is a perfect example of a "nice to have but completely unnecessary" feature. It has never been compelling enough to actually make it high enough on the list to be implemented. We have a list of "nice to have but completely unnecessary" features literally longer than your arm, and we can't implement more than a couple of them in any release, sorry!
因此,您想要输入一个随机方法名称,并希望编译器在所有已知名称空间(即所有正在“使用”的名称空间)中的所有类中搜索具有该名称的方法?为什么?
您可以从中创建一个扩展方法,这样您就可以调用 array1.Join(array2):
So you want to enter a random method name, and want the compiler to search through all classes in all known namespaces (i.e. all namespaces that are being "using'ed") for a method with that name? Why?
You could create an extension method from it, so you can call
array1.Join(array2)
:执行类似操作的标准方法是创建如下所示的扩展方法。不可能包含带有“using 语句”的类。
做一个这样的方法。
The standard way to do something similar is to make an extensionmethod like the one below. Include classes with "using statement" is not possible.
Make a method like this.
C# 不支持模块。
您可以使用 T[][] 的扩展方法,
这将让您执行此操作:
无论如何,我认为 Linq 支持这一点:
C# doesn't support modules.
You could use extension methods for T[][]
this will let you do this:
Anyway, I think this is supported by Linq: