Razor supports C# (C sharp) in ASP.NET. This section explains C# variables.
Variables are named entities used to store data.
Variables are used to store data.
A variable name must begin with an alphabetic character and cannot contain spaces or reserved characters. A variable can be of a specified type, indicating the type of data it stores. The string variable stores a string value ("Welcome to w3cschool.cn"), the integer variable stores a numeric value (103), the date variable stores a date value, and so on. Variables are declared using the var keyword, or by using a type if you want to declare a type, but ASP.NET can usually determine the data type automatically.
// Using the var keyword: var greeting = "Welcome to w3cschool.cn"; var counter = 103; var today = DateTime.Today; // Using data types: string greeting = "Welcome to w3cschool.cn"; int counter = 103; DateTime today = DateTime.Today;
Commonly used data types are listed below:
type | describe | Example |
---|---|---|
int | Integer (all numbers) | 103, 12, 5168 |
float | floating point number | 3.14, 3.4e38 |
decimal | Decimal numbers (high precision) | 1037.196543 |
bool | Boolean value | true, false |
string | string | "Hello w3cschool.cn", "John" |
Operators tell ASP.NET what commands to perform in an expression.
The C# language supports a variety of operators. Commonly used operators are listed below:
operator | describe | Example |
---|---|---|
= | Assign a value to a variable. | i=6 |
+ - */ | Add a value or a variable. Subtract a value or variable. Multiply a value or variable. Divide by a value or variable. | i=5+5 i=5-5 i=5*5 i=5/5 |
+= -= | The variable is incremented. Decrement the variable. | i += 1 i -= 1 |
== | equal. Returns true if the values are equal. | if (i==10) |
!= | No wait. Returns true if the values are not equal. | if (i!=10) |
< > <= >= | Less than. Greater than. Less than or equal to. Greater than or equal to. | if (i<10) if (i>10) if (i<=10) if (i>=10) |
+ | A connection string (a series of related things). | "w3" + "schools" |
. | Dot number. Separate objects and methods. | DateTime.Hour |
() | parentheses. Group values. | (i+5) |
() | parentheses. Pass parameters. | x=Add(i,5) |
[] | Square brackets. Access the values of an array or collection. | name[3] |
! | No. True/false negation. | if (!ready) |
&& || | Logical AND. Logical OR. | if (ready && clear) if (ready || clear) |
Converting from one data type to another is sometimes useful. The most common example is converting a string input to another type, such as an integer or date.
As a general rule, user input is treated as a string, even if the user enters a number. Numeric inputs must therefore be converted into numbers before they can be used in calculations.
Common conversion methods are listed below:
method | describe | Example |
---|---|---|
AsInt() IsInt() | Convert string to integer. | if (myString.IsInt()) {myInt=myString.AsInt();} |
AsFloat() IsFloat() | Convert a string to a floating point number. | if (myString.IsFloat()) {myFloat=myString.AsFloat();} |
AsDecimal() IsDecimal() | Convert a string to a decimal number. | if (myString.IsDecimal()) {myDec=myString.AsDecimal();} |
AsDateTime() IsDateTime() | Convert string to ASP.NET DateTime type. | myString="10/10/2012"; myDate=myString.AsDateTime(); |
AsBool() IsBool() | Convert a string to a Boolean value. | myString="True"; myBool=myString.AsBool(); |
ToString() | Convert any data type to string. | myInt=1234; myString=myInt.ToString(); |