473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Anonymous type etc. in C# 3.0

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);
}
}

Nov 17 '05 #1
17 1244
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);
}
}

Nov 17 '05 #2
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

Nov 17 '05 #3
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);
}

Nov 17 '05 #4
Chris Dunaway wrote:

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,

Nov 17 '05 #5
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 Stacey [MVP]

"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);
}
}

Nov 17 '05 #6
Chris,

From what I can tell, that should work. Very clever =)

As for the Select being defined, I pulled that from the C# 3.0 language
enhancements specification. You can find that at:

http://download.microsoft.com/downlo...cification.doc

It's defined as an extension method of IEnumerable<T>.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Chris Dunaway wrote:

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,

Nov 17 '05 #7
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?

Nov 17 '05 #8
Chris,

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

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

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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?

Nov 17 '05 #9
Nicholas Paldino [.NET/C# MVP] wrote:
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);
}
}

Nov 17 '05 #10
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.

--
William Stacey [MVP]

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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?

Nov 17 '05 #11
It compiles and runs fine.

--
William Stacey [MVP]

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eW**************@TK2MSFTNGP14.phx.gbl...
Chris,

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

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

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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?


Nov 17 '05 #12
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.

--
William Stacey [MVP]

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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);
}

Nov 17 '05 #13
Hi, how can you compile the C# 3.0 code? Where is the compiler?

Ab.

"William Stacey [MVP]" wrote:
It compiles and runs fine.

--
William Stacey [MVP]

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eW**************@TK2MSFTNGP14.phx.gbl...
Chris,

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

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

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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?



Nov 17 '05 #14
http://msdn.microsoft.com/netframework/future/linq/
See the "C# LINQ Tech Preview" link to download the msi.

--
William Stacey [MVP]

"Abubakar" <Ab******@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
Hi, how can you compile the C# 3.0 code? Where is the compiler?

Ab.

"William Stacey [MVP]" wrote:
It compiles and runs fine.

--
William Stacey [MVP]

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:eW**************@TK2MSFTNGP14.phx.gbl...
> Chris,
>
> I believe the example is wrong, see my response with the definition
> of
> Select.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mv*@spam.guard.caspershouse.com
>
> "Chris Dunaway" <du******@gmail.com> wrote in message
> news:11**********************@z14g2000cwz.googlegr oups.com...
>> 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?
>>
>
>


Nov 17 '05 #15

William Stacey [MVP] wrote:
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.

Nov 17 '05 #16
I think inside of the mojo they create behind the scenes like in an
enumerator or something.

--
William Stacey [MVP]

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

William Stacey [MVP] wrote:
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.

Nov 17 '05 #17
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]
- mv*@spam.guard.caspershouse.com

"William Stacey [MVP]" <st*****@mvps.org> wrote in message
news:eA*************@tk2msftngp13.phx.gbl...
It compiles and runs fine.

--
William Stacey [MVP]

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eW**************@TK2MSFTNGP14.phx.gbl...
Chris,

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

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

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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?



Nov 17 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mark Brandyberry | last post by:
I have a bit of a problem with an overloaded operator that I'm writing that I have distilled down to an example in the code below. Essentially, I have an operator function (+=) that takes a...
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
11
by: G Fernandes | last post by:
I've heard some mention "anonymous" objects in c.l.c and other places (conversations), and I was wondering what exactly these are. Anonymous seems to imply that there is no name, but no name...
0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
4
by: Harold Howe | last post by:
I am running into a situation where the compiler complains that it cannot infer the type parameters of a generic method when one of the function arguments is an anonymous method. Here is a...
7
by: Bill Woodruff | last post by:
I've found it's no problem to insert instances of named delegates as values into a generic dictionary of the form : private Dictionary<KeyType, DelegatemyDictionary = new Dictionary<KeyType,...
6
by: Gaijinco | last post by:
I have always felt that there are a lot of topics that you learned the facts but you only grasp the matter sometime down the road. For me, two of those topics are inner classes and anonymous...
4
by: Frankie | last post by:
I have just gotten up to speed on what anonymous methods are (syntax, capabilities, etc), and how they can be used with /called via delegates. What I am wondering is... 1. Are they only/mostly...
3
by: William Stacey [C# MVP] | last post by:
Maybe an Oxymoron, but this would be very useful. Anonomous types are great for doing projections (especially in Linq), but not being able to pass or return them is a bummer. Why couldn't they add...
8
by: =?Utf-8?B?UGFvbG8=?= | last post by:
Is there any way I can avoid the ugly and repetitive 'if' construct shown below? I can't refactor into a separate method because qryTrans is an anonymous type. I don't think I can use a switch...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.