为什么 ASP.net 使用方括号
我来自经典的 ASP,我做了:
myVar = request.querystring("ID")
response.redirect("lol.asp");
在 .net 中,它是:
myVar = Request.Querystring["ID"];
Response.Redirect("lol.aspx");
何时使用方括号而不是圆括号?它们代表什么意思?我现在有点理解它代表一个索引?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为 ASP Classic 是 Visual Basic Script,它派生自 Visual Basic 语法。
如果您想使用带有“圆括号”的 ASP.NET,只需在 ASP.NET 的代码隐藏中切换到 VB.NET。
“圆括号”或“方括号”是 VB.NET 和 C# 中任意的常规语法决定。
更新:我忘了提及 ASP Classic 也支持 JScript,因此带有 JScript 的 ASP classic 可以使用“方括号”访问数组索引,并且主要是索引器。但问题的作者似乎使用 ASP/VBScript :)
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 :)
方括号用于声明和访问具有元素计数或索引的数组。
http://msdn.microsoft.com/en -us/library/aa288453%28v=vs.71%29.aspx
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
这是C#和VB之间的区别。
This is a difference between C# and VB.
这是标准 C# 语言语法的一部分(可以追溯到 C 和其他语言)。
方括号
[]
用于访问数组或集合中的元素(NameValueCollection
(如果是Request.QueryString
)。在数组中,可以使用数字索引器访问元素,但在集合中,通常可以使用数字索引器或字符串按名称访问元素。
有关 C# 数组的教程,请查看 http:// msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
括号
()
用于包围传递给函数的参数(并且是必需的调用函数时,即使该函数不带参数)。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 (aNameValueCollection
in the case ofRequest.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).这是一篇旧文章,但它确实提供了 VB.NET 和 C# 之间的一些差异的比较。
在 Visual Basic .NET 和 Visual C# 中创建控件数组.NET
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
将我的两分钱添加到 Matías 正确答案和 Richard 正确信息中,
Request.Querystring
是经典 ASP 和 ASP.NET 中的字符串集合,因此这一切都取决于您如何访问某个项目收藏。在 C# 中,保留
()
来调用方法,因此 C# 中的Request.Querystring("ID")
将尝试调用Querystring
作为Request
的方法,传递"ID"
作为参数。要访问集合项,需要使用[]
- 并且 C# 对此一如既往地严格。VBScript 更加“灵活”,并且会自行检查 - 如果
QueryString
是一个集合,则()
意味着访问它并获取一个项目根据给定的索引器,否则尝试将其作为方法调用。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 havingRequest.Querystring("ID")
in C# will try to invokeQuerystring
as method ofRequest
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.