C#3.0中的匿名类型等 [英] Anonymous type etc. in C# 3.0

查看:42
本文介绍了C#3.0中的匿名类型等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解,基本上,这个C#3.0代码的作用,但我不清楚

如何确定lambda

表达式中num和index的数据类型在以下代码中。我也无法看到它如何将数组

索引到索引参数中?


顺便说一句:有没有更好的论坛来提出这些问题?

public void Linq12(){

int [] numbers = {5,4,1,3,9,8,6,7,2,0};


var numsInPlace = numbers.Select((num,index)=> new {Num = num,

InPlace =(num == index)});


Console.WriteLine(" Number:In-place?");

foreach(var n in numsInPlace){

Console.WriteLine(" {0}:{1}",n.Num,n.InPlace);

}

}

解决方案

Chris,


这是无效的,因为Select方法只需要一个lambda
带有一个参数的
表达式(正在评估选择的项目)。


如果你想这样做,你需要在枚举中输入一个类型

暴露了指数和价值。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Chris Dunaway" <杜****** @ gmail.com>在消息中写道

news:11 ********************** @ g49g2000cwa.googlegr oups.com ...

基本上,我理解这个C#3.0代码的作用,但是我不清楚如何在下面的代码中确定lambda
表达式中num和index的数据类型。我也无法看到它如何将数组
索引转换为索引参数?

顺便说一下:有没有更好的论坛可以提出这些问题?

public void Linq12 (){
int [] numbers = {5,4,1,3,9,8,6,7,2,0};

var numsInPlace = numbers.Select(( num,index)=> new {Num = num,
InPlace =(num == index)});

Console.WriteLine(" Number:In-place?" );;
foreach(var n in numsInPlace){
Console.WriteLine(" {0}:{1}",n.Num,n.InPlace);
}
}



Nicholas Paldino [.NET / C#MVP]写道:

这是无效的,因为Select方法只使用一个参数(要评估的项目进行选择)的lambda
表达式。




无效?我从MS的这个链接得到了这个例子:

http://msdn.microsoft.com/vcsharp/fu...t.aspx#indexed


< blockquote>它会这样工作吗? (注意我从lambda表达式中删除了索引参数

并在匿名

类型中使用了Array.IndexOf)


public void Linq12(){

int [] numbers = {5,4,1,3,9,8,6,7,2,0};

var numsInPlace = numbers.Select((num)=> new {Num = num,

InPlace =(num == Array.IndexOf(numbers,num))});


Console.WriteLine(" Number:In-place?");

foreach(var n in numsInPlace){

Console.WriteLine(" {0}:{1}",n.Num,n.InPlace);

}


I understand, basically, what this C# 3.0 code does, but I am unclear
on how it determines the data type of num and index in the lambda
expression in the following code. Nor can I see how it gets the array
index into the index parameter?

BTW: Is there a better forum for asking these questions?
public void Linq12() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var numsInPlace = numbers.Select((num, index) => new {Num = num,
InPlace = (num == index)});

Console.WriteLine("Number: In-place?");
foreach (var n in numsInPlace) {
Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
}
}

解决方案

Chris,

This is not valid, because the Select method will only take a lambda
expression with one parameter (the item being evaluated for selection).

If you wanted to do this, you would need a type in the enumeration which
exposed the index and the value.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

I understand, basically, what this C# 3.0 code does, but I am unclear
on how it determines the data type of num and index in the lambda
expression in the following code. Nor can I see how it gets the array
index into the index parameter?

BTW: Is there a better forum for asking these questions?
public void Linq12() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var numsInPlace = numbers.Select((num, index) => new {Num = num,
InPlace = (num == index)});

Console.WriteLine("Number: In-place?");
foreach (var n in numsInPlace) {
Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
}
}



Nicholas Paldino [.NET/C# MVP] wrote:

This is not valid, because the Select method will only take a lambda
expression with one parameter (the item being evaluated for selection).



Not valid? I got that example from this link at MS:

http://msdn.microsoft.com/vcsharp/fu...t.aspx#indexed


Would it work like this? (Note that I eliminated the index parameter
off of the lambda expression and used of Array.IndexOf in the anonymous
type)

public void Linq12() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

var numsInPlace = numbers.Select((num) => new {Num = num,
InPlace = (num == Array.IndexOf( numbers, num ))});

Console.WriteLine("Number: In-place?");
foreach (var n in numsInPlace) {
Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
}


这篇关于C#3.0中的匿名类型等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆