Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 15th, 2012, 02:15 PM
Friend of Wrox
Points: 1,905, Level: 17
Points: 1,905, Level: 17 Points: 1,905, Level: 17 Points: 1,905, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #2 (permalink)  
Old March 15th, 2012, 05:58 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #3 (permalink)  
Old March 15th, 2012, 07:35 PM
Friend of Wrox
Points: 1,905, Level: 17
Points: 1,905, Level: 17 Points: 1,905, Level: 17 Points: 1,905, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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!
Reply With Quote
  #4 (permalink)  
Old March 15th, 2012, 07:43 PM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #5 (permalink)  
Old March 15th, 2012, 08:07 PM
Friend of Wrox
Points: 1,905, Level: 17
Points: 1,905, Level: 17 Points: 1,905, Level: 17 Points: 1,905, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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?
Reply With Quote
  #6 (permalink)  
Old March 16th, 2012, 02:35 AM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
  #7 (permalink)  
Old March 16th, 2012, 05:25 AM
Imar's Avatar
Wrox Author
Points: 72,035, Level: 100
Points: 72,035, Level: 100 Points: 72,035, Level: 100 Points: 72,035, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,079
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:14 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.