tags:

views:

196

answers:

3

Are there any advantages of using one over the other besides readability?

+7  A: 

Nope, they do the same thing. Session["item"] is the same as Session.Item["item"].

Jeff Siver
+3  A: 

You will be accessing the same collection with either.

Its just a hang over from classic ASP, where the session object is implement as a COM object. COM could nominate one property as the default property (which typically would take an indexing parameter). In the case of Session the Item property is the default property.

In an effort to allow classic ASPers to port code to .NET the Session, Server, Request, Response and Application classes were crafted to be similar between classic and .NET ASP.

AnthonyWJones
A: 

Personally, I think referencing a property explicitly (Session.Item("Hello")) is always more readable than relying on that property's being the default (Session("Item")), but the readability is at the expense of more typing. If typing is a problem for you, switch to Ruby-on-Rails.

MusiGenesis
In this case "Item" is just an artifact of how indexers work in .Net. It's required for the [] syntax to work in C#. And your alternative for stylistic point where either option is support is "switch to ruby on rails"? Really?
Joel Coehoorn
As if there is any editor out there for RoR that could *ever* save you more keystrokes than Visual Studio does.
Josh Stodola
@Joel: Grrrrr!!! I hate it when my rep isn't a multiple of 5. Please go downvote 4 other answers that I couldn't care less about.
MusiGenesis
@Josh: I agree totally. Weirdly, the co-workers of mine who switched to RoR had turned off Intellisense in Visual Studio, claiming that it was a "useless crutch".
MusiGenesis
I'll help. All in good fun I hope :P
Joe Philllips
@Joel: no, not really. I'm not an RoR fan by any stretch of the imagination.
MusiGenesis
Damn you all. I'm never going to make it to page 3. :)
MusiGenesis
@d03boy: it was a lot of fun to downvote your question. Now I'm a multiple of 10! Whoo hoo! :P
MusiGenesis