Wrox Programmer Forums
|
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
 
Old March 15th, 2012, 02:15 PM
Friend of Wrox
 
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
Default 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.
 
Old March 15th, 2012, 05:58 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 15th, 2012, 07:35 PM
Friend of Wrox
 
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
Default 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!
 
Old March 15th, 2012, 07:43 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 15th, 2012, 08:07 PM
Friend of Wrox
 
Join Date: May 2011
Posts: 411
Thanks: 13
Thanked 7 Times in 7 Posts
Default What is the difference between.....

In LINQ between a .edmx file and a .dbml file?
 
Old March 16th, 2012, 02:35 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Heuh? That question doesn't make a lot of sense and seems quite out of context. Can you elaborate?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 16th, 2012, 05:25 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Oh, I noticed now that half of your question is in the Title field. I am subscribed to posts through e-mail where the Title field isn't shown.

.dbml ==> LINQ to SQL
.edmx ==> Entity Framework.

Google knows the differences: www.google.com/?q=difference+between+entity+framework+and+linq+to +sql

http://stackoverflow.com/questions/3...sql-by-net-4-0

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot use var in a application maanikmca Visual Studio 2005 16 November 16th, 2010 06:17 AM
var,text ,..... angelboy SQL Language 2 May 29th, 2007 11:37 AM
var as variable incredi-man Javascript 2 September 16th, 2006 11:46 AM
Using a var into the name of another var or field. guizero Access VBA 3 May 30th, 2006 03:47 PM
Temp Var in For Each pbernardo XSLT 5 November 14th, 2003 06:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.