|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Is type inference required with linq?I was reviewing some linq samples and came across this one: public void Linq5() { string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var shortDigits = digits.Where((digit, index) => digit.Length < index); Console.WriteLine("Short digits:"); foreach (var d in shortDigits) { Console.WriteLine("The word {0} is shorter than its value.", d); } } What exactly is the return type of the digits.Where call? Is shortDigits a list of strings? And if so, are you required to use type inference when using linq or can you explicitly specify the type? For example, could the line above be replaced with this: List<string> shortDigits = digits.Where((digit, index) => digit.Length < index); Similarly in the foreach line, since in this simple example, it's obvious that the type is a string could you use this: foreach (string d in shortDigits) I can't type this in to try it myself as I don't have VS2008 installed here (yet!). Thanks, Chris
Show quote
Hide quote
>I was reviewing some linq samples and came across this one: This is a VB group :)> >public void Linq5() { > string[] digits = { "zero", "one", "two", "three", "four", "five", >"six", "seven", "eight", "nine" }; > > var shortDigits = digits.Where((digit, index) => digit.Length < >index); > > Console.WriteLine("Short digits:"); > foreach (var d in shortDigits) { > Console.WriteLine("The word {0} is shorter than its value.", >d); > } >} >What exactly is the return type of the digits.Where call? Is The standard Where query operator returns an IEnumerable<T>, in this>shortDigits a list of strings? And if so, are you required to use >type inference when using linq or can you explicitly specify the type? case IEnumerable<string>. You're only required to use var when you can't specify the type yourself, i.e. when dealing with anonymous types. >Similarly in the foreach line, since in this simple example, it's Yes, and to me it makes no sense to replace string with var in this>obvious that the type is a string could you use this: > > foreach (string d in shortDigits) > case. Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. On Nov 20, 1:38 pm, Mattias Sjögren <mattias.dont.want.s***@mvps.org>
wrote: Thanks for the answers. > My apologies. That's twice I've posted in the wrong group today.> This is a VB group :) > I've been going back and forth and I wasn't where I thought I was. The holiday can't come soon enough!! Chris
Running vb.net 2005 app under windows restricted user
sql joins in VB Simple VB .Net question fast updates to textbox? Emailing a form in Visual Basic 2005 Express Need to develop an IDE add-on that would behave similar to the Find in Entire Solution colons Check if Mousepointer is over a region data to datagrid Problem Reading XML from Website |
|||||||||||||||||||||||