ASP. NET MVC Razor Syntax (2)

Source: Internet
Author: User
Tags alphabetic character servervariables

A variable is a named entity used to store data.

Variable

Variables are used to store data.

The variable name must begin with an alphabetic character and cannot contain spaces and reserved characters.

A variable can be a specific type that indicates the type of data it stores. String variables Store string values ("Welcome to W3school"), integer variables store numeric values (103), date variables store date values, and so on.

Variables are declared using the var keyword or type, but ASP. NET is usually able to determine the type of the data automatically.

Use var keyword: var greeting = "Welcome to W3school"; var counter = 103;var today = datetime.today;//using the data type: string greet ing = "Welcome to W3school"; int counter = 103;datetime today = Datetime.today;

Statements can be repeated in a loop.

For loop

If you need to run the same statement repeatedly, you can write a loop.

If you are able to determine the number of cycles, you can use the for loop . This type of loop is designed specifically for counting or reverse counting:

For Each loop

If you need to work with collections or arrays, you typically use the For each loop .

A collection is a set of similar objects that allow you to perform a task once on each project. The For Each loop iterates through the collection until it is finished.

The following example iterates through the ASP. NET Request.ServerVariables collection.

While loop

While is a general-purpose loop.

The while Loop begins with the keyword while, followed by parentheses, which defines the length of the loop, and then the block of code to loop.

The while loop usually increases or decreases the variable used for the count.

In the following example, each time the loop is run, the + = operator adds 1 to the variable i.

@{string[] Members = {' Jani ', ' hege ', ' Kai ', ' Jim '};int i = array.indexof (members, ' Kai ') +1;int len = members. Length;string x = members[2-1];} 

Array

If you need to store similar variables, but you don't want to create separate variables for each project, then the array comes in handy:

ASP. NET MVC Razor Syntax (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.