# NMath User's Guide

6.9 Matrix Enumeration (.NET, C#, CSharp, VB, Visual Basic, F#)

NMath matrix classes provide standard .NET GetEnumerator() methods for returning IEnumerator objects. For example:

Code Example – C# matrix

```int rows = 13, cols = 3;
var A = new DoubleMatrix( rows, cols, 0, .25 );
IEnumerator elements = A.GetEnumerator();

var data = new double[rows*cols];
i = 0;
while ( elements.MoveNext() )
{
data[i++] = (double) elements.Current;
}
```

Code Example – VB matrix

```Dim Rows As Integer = 13
Dim Cols As Integer = 3
Dim A As New DoubleMatrix(Rows, Cols, 0.0, 0.25)
Dim Elements As IEnumerator = A.GetEnumerator()

Dim Data(Rows * Cols) As Double

Dim I As Integer = 0
While Elements.MoveNext()
I += 1
Data(I) = CType(Elements.Current, Double)
End While
```

Note that the Current property on an IEnumerator returns the current object in the collection, which must then be cast to the appropriate type. NMath also provides custom strongly-typed enumerators: IFloatEnumerator, IDoubleEnumerator, IFloatComplexEnumerator, and IDoubleComplexEnumerator. These avoid casting, and are therefore much faster.

For instance:

Code Example – C# matrix

```int rows = 13, cols = 3;
var A = new DoubleMatrix( rows, cols, 0, .25 );
IDoubleEnumerator elements = A.GetDoubleEnumerator();

var data = new double[rows*cols];
i = 0;
while ( elements.MoveNext() )
{
data[i++] = elements.Current;       // No need to cast to double
}
```

Code Example – VB matrix

```Dim Rows As Integer = 13
Dim Cols As Integer = 3
Dim A As New DoubleMatrix(Rows, Cols, 0.0, 0.25)
Dim Elements As IDoubleEnumerator = A.GetDoubleEnumerator()

Dim Data(Rows * Cols) As Double
Dim I As Integer = 0
While Elements.MoveNext()
I += 1
Data(I) = Elements.Current ' No need to cast to Double
End While

```

Top

Top