如何使用自定义类型初始化数组

时间:2022-01-06 21:29:40

How do I initialize this array of custom types:

如何初始化此自定义类型数组:

PostType[] q = new PostType[qArray.Length];

//initialize array
for( int x = 0; x < qArray.Length; x++)
    q[x] = new PostType();

Is there a better way to initialize this array?

有没有更好的方法来初始化这个数组?

6 个解决方案

#1


5  

The way you are doing it is fine:

你这样做的方式很好:

PostType[] q = new PostType[qArray.Length];
for (int i = 0; i < q.Length; i++)
    q[i] = new PostType();

One thing I have changed are to rename the index veriable from x to i, as I find this easier to read, although it's a subjective thing.

我改变的一件事是将索引从x重命名为i,因为我觉得这更容易阅读,尽管这是一个主观的事情。

Another thing I have changed is the for loop end condition should depend on the length of q, not on the length of qArray. The reason for this is that with your method if you decide to change the first line to use a different length instead of qArray.Length, you'd have to remember to change the second line too. With the modified code you only need to update the first line of code and the rest will work without modification.

我改变的另一件事是for循环结束条件应该取决于q的长度,而不是qArray的长度。这样做的原因是,如果您决定将第一行更改为使用不同的长度而不是qArray.Length,则使用您的方法,您必须记住也要更改第二行。使用修改后的代码,您只需更新第一行代码,其余代码无需修改即可使用。

You could also do this using Linq:

您也可以使用Linq执行此操作:

PostType[] q = Enumerable.Range(0, qArray.Length)
                         .Select(_ => new PostType())
                         .ToArray();

But for large arrays this will be slower and not really easier to read in my opinion (especially if you haven't seen it before). I think I'd probably just stick with the first method if I were you.

但是对于大型阵列,在我看来这会更慢并且不容易阅读(特别是如果你以前没有看过它)。如果我是你,我想我可能会坚持使用第一种方法。

#2


2  

There's no better way if PostType is a class: you need to visit each position in the array and set it.

如果PostType是一个类,则没有更好的方法:您需要访问数组中的每个位置并进行设置。

If PostType is a struct, then you don't need to do anything at all: each position in the array is automatically initialised to the default value of the struct (all fields 0). (But if you wanted to use a nondefault constructor then you'd be back to the same position as with classes -- you'd need to visit each position and explicitly call the nondefault constructor.)

如果PostType是一个结构,那么你根本不需要做任何事情:数组中的每个位置都会自动初始化为struct的默认值(所有字段为0)。 (但是如果你想使用非默认构造函数,那么你将回到与类相同的位置 - 你需要访问每个位置并显式调用非默认构造函数。)

#3


2  

Piece of cake. Why not do something like this. It's concise.

小菜一碟。为什么不做这样的事情。它简洁明了。

PostType[] q = qArray.Select(i => new PostType()).ToArray();

#4


1  

There is actually no better way to do this. Refer to http://dotnetperls.com/initialize-array
There are several other methods, but simple loop is significally faster

实际上没有更好的方法来做到这一点。请参阅http://dotnetperls.com/initialize-array还有其他几种方法,但简单的循环显然更快

#5


1  

I have a good idea , an elegant way to initialize arrays custom type.

我有一个好主意,一种初始化数组自定义类型的优雅方法。

first you have to declare a static method to do this jobs in your PostType class or in a new class.

首先,您必须声明一个静态方法来在PostType类或新类中执行此作业。

 public class PostType
{
    public static T[] extent<T>(int n)
    {
        T[] result = new T[n];

        if (!typeof(T).IsValueType)
        {
            Type type = typeof(T);
            object objTSource = Activator.CreateInstance(type);

            for (int i = 0; i < n; i++)
            {
                result[i] = (T)objTSource;
            }
        }

        return result;
    }
}

Finally, it's an elegant way:

最后,这是一种优雅的方式:

PostType[] q = PostType.extent<PostType>(qArray.Length);

#6


0  

Not that I can think off, when you create the array memory is located for it, other than that every index points to null. The best way to make your code more efficient would be to store qArray.Length into a variable so you don't call .length on every iteration of loop i.e.

不是我能想到的,当你为它创建数组内存时,除了每个索引都指向null。使代码更高效的最佳方法是将qArray.Length存储到变量中,这样就不会在每次循环迭代时调用.length,即

int a = qLength
    for( int x = 0; x < qLength; x++)
        q[x] = new PostType();

Note: Enumerable Repeat will not work as it creates one instance of the object and then repeats a reference to this object.

注意:Enumerable Repeat不起作用,因为它创建了一个对象实例,然后重复对该对象的引用。

#1


5  

The way you are doing it is fine:

你这样做的方式很好:

PostType[] q = new PostType[qArray.Length];
for (int i = 0; i < q.Length; i++)
    q[i] = new PostType();

One thing I have changed are to rename the index veriable from x to i, as I find this easier to read, although it's a subjective thing.

我改变的一件事是将索引从x重命名为i,因为我觉得这更容易阅读,尽管这是一个主观的事情。

Another thing I have changed is the for loop end condition should depend on the length of q, not on the length of qArray. The reason for this is that with your method if you decide to change the first line to use a different length instead of qArray.Length, you'd have to remember to change the second line too. With the modified code you only need to update the first line of code and the rest will work without modification.

我改变的另一件事是for循环结束条件应该取决于q的长度,而不是qArray的长度。这样做的原因是,如果您决定将第一行更改为使用不同的长度而不是qArray.Length,则使用您的方法,您必须记住也要更改第二行。使用修改后的代码,您只需更新第一行代码,其余代码无需修改即可使用。

You could also do this using Linq:

您也可以使用Linq执行此操作:

PostType[] q = Enumerable.Range(0, qArray.Length)
                         .Select(_ => new PostType())
                         .ToArray();

But for large arrays this will be slower and not really easier to read in my opinion (especially if you haven't seen it before). I think I'd probably just stick with the first method if I were you.

但是对于大型阵列,在我看来这会更慢并且不容易阅读(特别是如果你以前没有看过它)。如果我是你,我想我可能会坚持使用第一种方法。

#2


2  

There's no better way if PostType is a class: you need to visit each position in the array and set it.

如果PostType是一个类,则没有更好的方法:您需要访问数组中的每个位置并进行设置。

If PostType is a struct, then you don't need to do anything at all: each position in the array is automatically initialised to the default value of the struct (all fields 0). (But if you wanted to use a nondefault constructor then you'd be back to the same position as with classes -- you'd need to visit each position and explicitly call the nondefault constructor.)

如果PostType是一个结构,那么你根本不需要做任何事情:数组中的每个位置都会自动初始化为struct的默认值(所有字段为0)。 (但是如果你想使用非默认构造函数,那么你将回到与类相同的位置 - 你需要访问每个位置并显式调用非默认构造函数。)

#3


2  

Piece of cake. Why not do something like this. It's concise.

小菜一碟。为什么不做这样的事情。它简洁明了。

PostType[] q = qArray.Select(i => new PostType()).ToArray();

#4


1  

There is actually no better way to do this. Refer to http://dotnetperls.com/initialize-array
There are several other methods, but simple loop is significally faster

实际上没有更好的方法来做到这一点。请参阅http://dotnetperls.com/initialize-array还有其他几种方法,但简单的循环显然更快

#5


1  

I have a good idea , an elegant way to initialize arrays custom type.

我有一个好主意,一种初始化数组自定义类型的优雅方法。

first you have to declare a static method to do this jobs in your PostType class or in a new class.

首先,您必须声明一个静态方法来在PostType类或新类中执行此作业。

 public class PostType
{
    public static T[] extent<T>(int n)
    {
        T[] result = new T[n];

        if (!typeof(T).IsValueType)
        {
            Type type = typeof(T);
            object objTSource = Activator.CreateInstance(type);

            for (int i = 0; i < n; i++)
            {
                result[i] = (T)objTSource;
            }
        }

        return result;
    }
}

Finally, it's an elegant way:

最后,这是一种优雅的方式:

PostType[] q = PostType.extent<PostType>(qArray.Length);

#6


0  

Not that I can think off, when you create the array memory is located for it, other than that every index points to null. The best way to make your code more efficient would be to store qArray.Length into a variable so you don't call .length on every iteration of loop i.e.

不是我能想到的,当你为它创建数组内存时,除了每个索引都指向null。使代码更高效的最佳方法是将qArray.Length存储到变量中,这样就不会在每次循环迭代时调用.length,即

int a = qLength
    for( int x = 0; x < qLength; x++)
        q[x] = new PostType();

Note: Enumerable Repeat will not work as it creates one instance of the object and then repeats a reference to this object.

注意:Enumerable Repeat不起作用,因为它创建了一个对象实例,然后重复对该对象的引用。