开发者

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 ro开发者_运维百科und ones? What do they signify? I'm sort of understanding it at the moment to represent an index?


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 :)


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


This is a difference between C# and VB.


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).


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜