如何在C#中使用数组类创建自定义数组 [英] How to create a Custom Array withou using Array Class in C#

查看:112
本文介绍了如何在C#中使用数组类创建自定义数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class MyArray<T>
    {
        T[] arr = null;
        public MyArray()
        {
            arr = new T[1];
            // myInitialize(ref arr, arr.Length + 1);
        }
        public void Add(T item)
        {
            arr[arr.Length - 1] = item;
            myInitialize(ref arr, arr.Length + 1);
        }
        private void myInitialize(ref T[] arr, int newLength)
        {
            if (arr == null)
            {
                arr = new T[newLength];
            }
            else
            {
                T[] tempArr = new T[newLength];
                //Array.Copy(arr, tempArr, arr.Length);
                for (int i = 0; i < newLength; i++)
                    if (i < arr.Length)
                    {
                        tempArr[i] = arr[i];
                    }
                    else
                    {
                        tempArr[i] = default(T);
                    }
                arr = new T[newLength];
                //Array.Copy(tempArr, arr, arr.Length);
                for (int i = 0; i < newLength; i++)
                {
                    arr[i] = tempArr[i];
                }
            }
        }
    }


现在我们可以测试上面创建的通用数组的任何类型.字符串类型,例如:在
下给出


Now we can test the above created generic Array for any type. string type e.g: is given below

public  static void Main(string[] args)
 {
     MyArray<string> obj = new MyArray<string>();
     obj.Add("1");
     obj.Add("2");
     obj.Add("3");
     obj.Add("4");
     Console.WriteLine();

 }

推荐答案

无论何时创建任何实数组,其基类始终为System.Array.您为什么要避免使用它?无论您是否查看代码示例,我都看不到.

例如,如果您想要模式动态行为(例如添加/删除元素),请考虑System.Collections.Generic.Array.另外,您可以实现自己的类或结构,并使用索引属性("this"成员,不要与实例引用"this"混淆)来公开类似数组的接口.您还可以创建实现System.Collections.IList接口的非数组类型.

—SA
Whenever you create any real array, its base class is always System.Array. Why would you want to avoid using it? I see no point, looking at your code sample or not.

If you, for example, want mode dynamic behavior, such as adding/removing elements, consider System.Collections.Generic.Array. Alternatively, you can implement your own classes or structures and expose array-like interface using indexed properties ("this" members, not to be confused with instance reference "this"). You can also create non-array type implementing System.Collections.IList interface.

—SA


根据您的代码,您只想使用List<T>:
According to your code, you just want to use List<T>:
public static void Main(string[] args)
{
    List<string> obj = new List<string>();
    obj.Add("1");
    obj.Add("2");
    obj.Add("3");
    obj.Add("4");
    Console.WriteLine();
}


在大多数情况下,您都希望使用List< T> ;,如Oliver所说.

如果要在运行时动态创建数组,可以使用 Array.CreateInstance [^ ].但是,由于您不知道类型,因此您可能必须将其存储在Array类型的变量中.如果您尝试使用反射/后期绑定来调用带有数组参数的方法,这是非常有用的,这非常罕有.
For most purposes, you want List<T>, as Oliver says.

If you want to create an array dynamically at runtime, it is possible, using Array.CreateInstance[^]. However, since you don''t know the type, you are likely to have to store this in a variable of type Array. It''s only really useful to go down this route if you are trying to use reflection/late binding to call methods that take array arguments, which is pretty rare.


这篇关于如何在C#中使用数组类创建自定义数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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