 |
BOOK: Beginning ASP.NET 4 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

March 15th, 2012, 02:15 PM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
The var, Inumerable question?
Considering the following code snippet that is somewhat along the lines of what you are explaining in pages 486 of your book:
Code:
int[] differentNums = {1, 2, 3, 4, 5};
Inumerable <int> doubleNumbers = from i in differentNums
select i * 2;
var squaredNums = from i in differentNums
select i * i;
for each (int number in squaredNums)
Response.write (number + "<br />);
The question I have at this pont is Inumerable acting like a generic typecast? What about the var declaration does it act the same or different than a generic typecast variable? Can the var declaration be used for floats, intergers, strings ect?
Thanks once again.
|
|

March 15th, 2012, 05:58 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Inumerable acting like a generic typecast? What about the var declaration does it act the same or different than a generic typecast variable? Can the var declaration be used for floats, intergers, strings ect?
|
No, No, Yes
IEnumerable of T means "a bunch of T" you can loop over. It's the minimal interface of a collection. While looping over it, each item is of type T (an int, a string, or whatever object you have).
var just means "hey, compiler, figure out the type I assign to the object". In other words, from the compiler's perspective, the following are equal:
string firstName = "Imar";
var firstName = "Imar";
In both cases, firstName is a string. In the first case, I explicitly defined it as one, in the second case the compiler inferred it for me. The same works for any other data type. E.g.:
int whatsTheAnswer = 42;
var whatsTheAnswer = 42;
In many cases, where you see var you can use the strongly typed name instead. Where var is required though is with anonymous types; e.g. for cases where you can't explicity state the variable type. Develpers tend to overuse var because they don't want to specify the type. I, for one, prefer the first variation of each of the last two examples as they are more explicit.
Your LINQ query returns a collection of ints so it fits the IEnumerable<int> data type....
Hope this helps,
Imar
|
|

March 15th, 2012, 07:35 PM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
Var overhead
Using Var instead of int, float, string etc what is the impact that using those two types of variable declarations have on memory allocation in specific to stack and heap allocations? In other words how do stack and heap memory management/allocations relate to var and a standard variable declaration like int, string or float?
Thanks again!
|
|

March 15th, 2012, 07:43 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Like I said, they are identical. With var, the compiler infers the type for you so you don't have to specify it. But I would recommend avoiding var where you can. Being explicit is better...
http://stackoverflow.com/questions/4...ord-in-c-sharp
Cheers,
Mar
|
|

March 15th, 2012, 08:07 PM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
|
|
What is the difference between.....
In LINQ between a .edmx file and a .dbml file?
|
|

March 16th, 2012, 02:35 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Heuh? That question doesn't make a lot of sense and seems quite out of context. Can you elaborate?
Imar
|
|
 |
|