Anonymous type etc. in C# 3.0

  • Thread starter Thread starter Chris Dunaway
  • Start date Start date
C

Chris Dunaway

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.
 
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);
}
 
Chris said:
var numsInPlace = numbers.Select((num) => new {Num = num,
InPlace = (num == Array.IndexOf( numbers, num ))});

And as a follow up, where is "Select" defined? Is that now available
because of Linq? Is it usable on all types?

Thanks,
 
I think internally it creates an anonymous type like below:

internal class ???
{
public int Num;
public bool InPlace;
}

And returns a collection of that type.
It inferes the class fields/properties easily because it knows num is of
type Int32 and it knows InPlace is a bool because of the expression.
 
William said:
It inferes the class fields/properties easily because it knows num is of
type Int32 and it knows InPlace is a bool because of the expression.

I understand that part, but how can it infer the type of "index"?
Where does that come from?
 
Chris,

I believe the example is wrong, see my response with the definition of
Select.
 
I think because the selector constructor calls for an Int in the second
overload.
(T)
or
(T, int)
So "index" could be called anything. It is a place holder so you can get
the index.
 
It compiles and runs fine.

--
William Stacey [MVP]

Nicholas Paldino said:
Chris,

I believe the example is wrong, see my response with the definition of
Select.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris Dunaway said:
I understand that part, but how can it infer the type of "index"?
Where does that come from?
 
Nicholas said:
Chris,

From what I can tell, that should work. Very clever =)
I assume that the examples are just to illustrate the concepts because
the following code seems easier to read (even with the cast):

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

Console.WriteLine("Number: In-place?");
foreach (int n in numbers) {
bool InPlace = (n == (int)Array.IndexOf(numbers,n));
Console.WriteLine("{0}: {1}", n, InPlace);
}
}
 
Yes but less efficient I think. IndexOf() requires another search into the
collection. If you use the index parm, then it set while it is traversing
the list.
 
Hi, how can you compile the C# 3.0 code? Where is the compiler?

Ab.

William Stacey said:
It compiles and runs fine.

--
William Stacey [MVP]

Nicholas Paldino said:
Chris,

I believe the example is wrong, see my response with the definition of
Select.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris Dunaway said:
William Stacey [MVP] wrote:
It inferes the class fields/properties easily because it knows num is of
type Int32 and it knows InPlace is a bool because of the expression.


I understand that part, but how can it infer the type of "index"?
Where does that come from?
 
http://msdn.microsoft.com/netframework/future/linq/
See the "C# LINQ Tech Preview" link to download the msi.

--
William Stacey [MVP]

Abubakar said:
Hi, how can you compile the C# 3.0 code? Where is the compiler?

Ab.

William Stacey said:
It compiles and runs fine.

--
William Stacey [MVP]

in
message news:[email protected]...
Chris,

I believe the example is wrong, see my response with the definition
of
Select.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

William Stacey [MVP] wrote:
It inferes the class fields/properties easily because it knows num is
of
type Int32 and it knows InPlace is a bool because of the expression.


I understand that part, but how can it infer the type of "index"?
Where does that come from?
 
William said:
I think because the selector constructor calls for an Int in the second
overload.
(T)
or
(T, int)
So "index" could be called anything. It is a place holder so you can get
the index.

But where does it get its value? Somehow, the array index gets stuffed
into that variable.
 
I think inside of the mojo they create behind the scenes like in an
enumerator or something.
 
William, Chris,

I've figured out why this works. There is an overload of the Select
method that looks like this:

public static IEnumerable<S> Select<T, S>(this IEnumerable<T> source,
Func<T, int, S> selector)
{
int index = 0;

foreach (T element in source)
{
yield return selector(element, index);
index++;
}
}

This is where the index comes from, and the overload that accepts a
lambda expression with two inputs (the second one being the index).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

William Stacey said:
It compiles and runs fine.

--
William Stacey [MVP]

Nicholas Paldino said:
Chris,

I believe the example is wrong, see my response with the definition of
Select.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Chris Dunaway said:
William Stacey [MVP] wrote:
It inferes the class fields/properties easily because it knows num is
of
type Int32 and it knows InPlace is a bool because of the expression.


I understand that part, but how can it infer the type of "index"?
Where does that come from?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top