c# - 在 C# 中将二维数组归零的最快方法

标签 c# .net arrays performance multidimensional-array

我有一个二维数组,我想将其清除并重置为 0 值。我知道如何使用 Array.Clear() 清除向量(一维数组),但我不知道清除二维矩阵的最佳方法。

double D = new double[10];  
Array.Clear(D, 0, D.Length);

如何清除一个 2D N x M 数组

double D[,] = new double[N,M];

感谢您提供的任何帮助。

最佳答案

Array.Clear也适用于多维数组:

double[,] D = new double[M,N];
Array.Clear(D, 0, D.Length);

注意,不需要自己计算长度,因为 Length属性返回元素的总数,而不管维数:

A 32-bit integer that represents the total number of elements in all the dimensions of the Array; zero if there are no elements in the array.

关于c# - 在 C# 中将二维数组归零的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19669482/

相关文章:

c# - Entity Framework Code First - 两个名称相同但 namespace 不同的实体

c# - 如何在 WPF 中更改每列项目的字体大小

.NET 生成服务引用时加载错误的程序集

c# - 找不到方法 : 'Void System.Diagnostics.Tracing.FrameworkEventSource.BeginGetResponse(System.Object, System.String)'

c# - 将数组转换为 IEnumerable<T>

c# - 如何使用非托管导出 (Robert Giesecke) 将 dateTime 类型数组从 .NET 传递到 Delphi?

arrays - Array append() 返回一个新数组

c# - 按邮政编码查找产品 |半正弦算法 |表现

c# - 通过 C# 转义 SQL INSERT INTO 中的特殊字符

.net - SQL Server 2008 R2 加密 - 使用 Entity Framework