Pra*_*ter 4 c# vb.net arrays clr
由于我仍然不理解的原因(参见这个SO问题)CLR中的多维数组没有实现IEnumerable<T>.所以以下不编译:
var m = new int[2,2] {{1, 2}, {3, 4}};
var q = from e in m select e;
Run Code Online (Sandbox Code Playgroud)
那怎么会在VB.NET中运行得很好呢?
Sub Main()
Dim m(,) As Integer = {{1, 2}, {3, 4}}
Dim q = From e In m Select e
For Each i In q
Console.WriteLine(i)
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
更新:
以下代码有效,因为C#编译器替换了foreachwith for循环以遍历每个维度.
foreach(var e in m)
Console.WriteLine(e);
Run Code Online (Sandbox Code Playgroud)
变
int[,] numArray3 = new int[,] { { 2, 2 }, { 3, 3 } };
int upperBound = numArray3.GetUpperBound(0);
int num4 = numArray3.GetUpperBound(1);
for (int i = numArray3.GetLowerBound(0); i <= upperBound; i++)
{
for (int j = numArray3.GetLowerBound(1); j <= num4; j++)
{
int num = numArray3[i, j];
Console.WriteLine(num);
}
}
Run Code Online (Sandbox Code Playgroud)
该查询在VB.Net中工作,因为它被转换为
IEnumerable<object> q = m.Cast<object>().Select<object, object>(o => o);
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为你可以调用Cast<TResult>()on IEnumerable,它[*,*]实现.
LINQ查询在C#中不起作用,因为C#和VB.Net设计者采用了不同的方法.VB.Net采用更多的手持方法并修复您的错误并转换IEnumerable为IEnumerable<object>可以使用它.
在C#中,您可以通过使用来模拟它
var q = from e in m.Cast<object>() select e;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2457 次 |
| 最近记录: |