在Asp.Net Razor中使用数组,下面的代码先定义一个数组,然后通过foreach循环进行遍历
@{ string[] members = {"Jani", "Hege", "Kai", "Jim"}; int i = Array.IndexOf(members, "Kai")+1; int len = members.Length; string x = members[2-1]; } <html> <body> <h3>Members</h3> @foreach (var person in members) { <p>@person</p> } <p>The number of names in Members are @len</p> <p>The person at position 2 is @x</p> <p>Kai is now in position @i</p> </body> </html>
页面执行后输出
Members Jani Hege Kai Jim The number of names in Members are 4 The person at position 2 is Hege Kai is now in position 3