473,326 Members | 2,134 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,326 software developers and data experts.

Is type inference required with linq?

When using linq queries, is it *required* to use type inference?

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<stringshortDigits = 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
Nov 20 '07 #1
2 1239

>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);
}
}
This is a VB group :)

>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?
The standard Where query operator returns an IEnumerable<T>, in this
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
obvious that the type is a string could you use this:

foreach (string d in shortDigits)
Yes, and to me it makes no sense to replace string with var in this
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.
Nov 20 '07 #2
On Nov 20, 1:38 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:

Thanks for the answers.
>
This is a VB group :)
My apologies. That's twice I've posted in the wrong group today.
I've been going back and forth and I wasn't where I thought I was.
The holiday can't come soon enough!!

Chris
Nov 20 '07 #3

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

Similar topics

3
by: A.M | last post by:
Hi, Is there any inelisence IDE (like visual studio) for python? Thanks, Alan
0
by: Brett C. | last post by:
My thesis, "Localized Type Inference of Atomic Types in Python", was successfully defended today for my MS in Computer Science at the California Polytechnic State University, San Luis Obispo. With...
1
by: Ken Cox [Microsoft MVP] | last post by:
Hi, I'm using a dataset to read data from an XML file. I don't control the XML. The XML has no schema and therefore is treated like a string. My data consumer wants the column type to be a...
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...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
4
by: Nie longhai | last post by:
I have a generic method in a class as follow: T Get<T>(string key) { ..... return Convert.ChangeType(v, typeof(T)) } when I use it in my code:
12
by: =?Utf-8?B?Z3V5?= | last post by:
What do people here think about VB2008 having the ability to infer type? e.g you can now say Dim i=5 which will create i as an integer Personally I do not like it as it is not consistent and may...
3
by: Masa Ito | last post by:
I am really jazzed about LINQ - but am struggling with some basics - like when does it infer the datatype? Simple example - I want to get all the tables in a typed dataset that have "RAW" in the...
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.