Is it possible to extend arrays in C#?

前端 未结 3 849
情歌与酒
情歌与酒 2020-12-10 03:09

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 IEnumera

3条回答
  •  [愿得一人]
    2020-12-10 03:27

    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());
        }
    }
    

提交回复
热议问题