我可以在 C# 中包含一个静态类,这样我就不必键入类名来使用其静态函数吗?

发布于 2024-12-27 14:10:44 字数 696 浏览 1 评论 0原文

在 VB.NET 中,我有一个名为 ArrayExtensions 的静态类,其中包含诸如 Join(Of T)(ParamArray arrays As T()()) As T() 之类的静态函数。他们在一个模块中。我不需要包含该模块;我可以从项目中的任何其他类中输入 Join(array1, array2, array3)

现在我正在尝试 C#,我在静态类 ArrayExtensions 中有相同的静态函数 T[] Join(params T[][] arrays) 。我似乎无法弄清楚如何避免每次都必须编写 ArrayExtensions.Join(array1, array2, array3) 。我尝试使用 ArrayExtensions ,但与 VB.NET 不同,它不起作用。

有什么方法可以在 C# 中输入 Join(array1, array2, array3) 吗?

我曾考虑将其更改为 T[] Join(this T[][] arrays) 并使用 {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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜妞爱困 2025-01-03 14:10:44

有什么方法可以在 C# 中输入 Join(array1, array2, array3) 吗?

不幸的是,没有。这是一个相当频繁请求的功能。如果能够做类似的事情那就太好了:

using System; // the namespace
using System.Math; // the type
    ...
    x = Sin(Abs(Max(...

而不是

    x = Math.Sin(Math.Abs(Math.Max(...

这是“很好有但完全不必要”功能的完美示例。它从来没有足够引人注目,以至于无法真正将其排在需要实施的清单上足够高的位置。我们有一个“很好但完全没有必要”的功能列表,这些功能实际上比您的手臂还长,并且我们无法在任何版本中实现超过几个功能,抱歉!

Is there any way I can just type Join(array1, array2, array3) in C#?

Unfortunately, no. This is a fairly frequently requested feature. It would be nice to be able to do something like:

using System; // the namespace
using System.Math; // the type
    ...
    x = Sin(Abs(Max(...

instead of

    x = Math.Sin(Math.Abs(Math.Max(...

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!

小鸟爱天空丶 2025-01-03 14:10:44

因此,您想要输入一个随机方法名称,并希望编译器在所有已知名称空间(即所有正在“使用”的名称空间)中的所有类中搜索具有该名称的方法?为什么?

您可以从中创建一个扩展方法,这样您就可以调用 array1.Join(array2):

T[] Join<T>(this T[][] array1, T[][] 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):

T[] Join<T>(this T[][] array1, T[][] array2)
猥︴琐丶欲为 2025-01-03 14:10:44

执行类似操作的标准方法是创建如下所示的扩展方法。不可能包含带有“using 语句”的类。

做一个这样的方法。

public static class ArrayExtensions
{
   public static Array MyOwnArrayJoin(this Array array1, Array array2)
   {
       //Do the Join and return Array
   }
}

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.

public static class ArrayExtensions
{
   public static Array MyOwnArrayJoin(this Array array1, Array array2)
   {
       //Do the Join and return Array
   }
}
枫林﹌晚霞¤ 2025-01-03 14:10:44

C# 不支持模块。
您可以使用 T[][] 的扩展方法,

public static class ArrayExtension 
{
    public static T[] Join<T>(this T[][] self, params T[][] arrays)
    {
        ...
    }
}

这将让您执行此操作:

array1.Join( array2, array3 );

无论如何,我认为 Linq 支持这一点:

array1.Union(array2).Union(array3);

C# doesn't support modules.
You could use extension methods for T[][]

public static class ArrayExtension 
{
    public static T[] Join<T>(this T[][] self, params T[][] arrays)
    {
        ...
    }
}

this will let you do this:

array1.Join( array2, array3 );

Anyway, I think this is supported by Linq:

array1.Union(array2).Union(array3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文