是否有可能延长阵列在C#? [英] Is it possible to extend arrays in C#?

查看:173
本文介绍了是否有可能延长阵列在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来添加方法外部类,如IEnumerable的。但是,我们能够扩展阵列在C#?

我打算一个方法添加到阵列将其转换为IEnumerable的,即使它是多维的。

不相关的<一个href="http://stackoverflow.com/questions/628427/how-to-extend-arrays-in-c">http://stackoverflow.com/questions/628427/how-to-extend-arrays-in-c

解决方案

 静态类扩展
{
    公共静态字符串扩展(该阵列阵列)
    {
        返回是的,你可以;
    }
}

类节目
{

    静态无效的主要(字串[] args)
    {
        INT [,,,] multiDimArray =新INT [10,10,10,10]
        Console.WriteLine(multiDimArray.Extend());
    }
}
 

I'm used to add methods to external classes like IEnumerable. But can we extend Arrays in C#?

I am planning to add a method to arrays that converts it to a IEnumerable even if it is multidimensional.

Not related to http://stackoverflow.com/questions/628427/how-to-extend-arrays-in-c

解决方案

static class Extension
{
    public static string Extend(this Array array)
    {
        return "Yes, you can";
    }
}

class Program
{

    static void Main(string[] args)
    {
        int[,,,] multiDimArray = new int[10,10,10,10];
        Console.WriteLine(multiDimArray.Extend());
    }
}

这篇关于是否有可能延长阵列在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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