Tom Gullen
Tom Gullen

Reputation: 61755

Why does ASP.net use square brackets

I'm coming from classic ASP and I did:

myVar = request.querystring("ID")
response.redirect("lol.asp");

And in .net it is:

myVar = Request.Querystring["ID"];
Response.Redirect("lol.aspx");

When are square brackets used over round ones? What do they signify? I'm sort of understanding it at the moment to represent an index?

Upvotes: 2

Views: 1598

Answers (6)

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

Adding my two cents to Matías correct answer and Richard correct information, the Request.Querystring is collection of strings, both in classic ASP and in ASP.NET so it all goes down to how you access an item of collection.

In C# the () are preserved to call a method so having Request.Querystring("ID") in C# will try to invoke Querystring as method of Request passing "ID" as argument. To access collection items, the [] are required instead - and C# is as usual strict about it.

VBScript is more "flexible", and will check by itself - if QueryString is a collection then the () means accessing it and getting an item according to the given indexer otherwise try invoking it as a method.

Upvotes: 1

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

Because ASP Classic is Visual Basic Script, which derives from Visual Basic syntax.

If you'd like to use ASP.NET with "round brackets", just switch to VB.NET in ASP.NET's code-behind.

"Round" or "square" brackets are an arbitrary, conventional syntax decision in VB.NET and C#.

UPDATE: I forgot to mention ASP Classic supports JScript too, so ASP classic with JScript would access to array indexes and, mainly indexers, with "square brackets". But it seems that question's author worked with ASP/VBScript :)

Upvotes: 7

Richard Ev
Richard Ev

Reputation: 54127

This is part of the standard C# language syntax (which can be traced back to C, and other languages).

Square brackets [] are used to access an element in an array or collection (a NameValueCollection in the case of Request.QueryString).

In an array elements are accessed with a numeric indexer, but in a collection you can typically use a numeric indexer or a string to access an element by name.

For a tutorial on C# arrays check out http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

Parentheses () are used to surround the arguments passed to a function (and are required when calling a function, even if that function takes no arguments).

Upvotes: 2

MattC
MattC

Reputation: 4004

It's an old article but it does offer a comparison of some of the differences between VB.NET and C#.

Creating Control Arrays in Visual Basic .NET and Visual C# .NET

Upvotes: 1

Richard Adnams
Richard Adnams

Reputation: 3128

The square brackets are used to declare and access an array with an element count or index.

http://msdn.microsoft.com/en-us/library/aa288453%28v=vs.71%29.aspx

Upvotes: 3

Robert Levy
Robert Levy

Reputation: 29083

This is a difference between C# and VB.

Upvotes: 2

Related Questions