简体   繁体   English

如何初始化已经在C#中声明的数组?

[英]How to initialize an array which has already declared in C#?

I have already declared an array: 我已经声明了一个数组:

double[,] g_bessel = new double[2, 2];

And, I want to initialize it at another place: 而且,我想在另一个地方初始化它:

{{1,2},{3,4}}

How can I initialize the matrix at a once and not cell by cell? 如何一次而不是逐个单元地初始化矩阵?

I mean something like this: 我的意思是这样的:

g_bessel = {{1,2},{3,4}};

You must use operator "new" for initialize new array in this way. 您必须使用运算符“ new”以这种方式初始化新数组。

   g_besse = new double[,]{{1,2},{3,4}} ;

This code allocates new memory for array. 此代码为数组分配新的内存。 If you need reinitialize this array many times, you can create custom method for change array's values. 如果您需要多次重新初始化此数组,则可以为更改数组的值创建自定义方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM