Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET Razor C# Variables

ASP.NET Razor - C# Variables


A variable is a named entity used to store data.


Variables

Variables are used to store data.

A variable name must begin with a letter character and cannot contain spaces or reserved characters.

A variable can be a specified type, representing the type of data it stores. A string variable stores string values ("Welcome to RUNOOB.COM"), an integer variable stores numeric values (103), a date variable stores date values, and so on.

Variables are declared using the var keyword, or by using a type (if you want to declare the type), but ASP.NET can usually determine the data type automatically.

Example

// Using the var keyword:

var greeting = "Welcome to RUNOOB.COM";

var counter = 103;

var today = DateTime.Today;

// Using data types:

string greeting = "Welcome to RUNOOB.COM";

int counter = 103;

DateTime today = DateTime.Today;


Data Type

The commonly used data types are listed below:

Type Description Example
int Integer (whole number) 103, 12, 5168
float Float (floating point number) 3.14, 3.4e38
decimal Decimal (high precision) 1037.196543
bool Boolean true, false
string String "Hello RUNOOB.COM", "John"

operator

Operators tell ASP.NET what command to execute in an expression.

The C# language supports various operators. The commonly used operators are listed below:

operator Description Example
= Assigns a value to a variable. i=6
+
 -       

 *       

 / | 加上一个值或者一个变量。       

 减去一个值或者一个变量。       

 乘以一个值或者一个变量。       

 除以一个值或者一个变量。 | i=5+5       

 i=5-5       

 i=5*5       

 i=5/5 |

| +=
-= | Variable increment.
Variable decrement. | i += 1
i -= 1 | | == | Equal. Returns true if the values are equal. | if (i==10) | | != | Not equal. 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) | | + | Concatenates strings (a series of related things). | "run" + "oob" | | . | Dot. Separates an object and its method. | DateTime.Hour | | () | Parentheses. Groups values. | (i+5) | | () | Parentheses. Passes parameters. | x=Add(i,5) | | [] | Brackets. Accesses values in an array or collection. | name[3] | | ! | Not. Inverts true/false. | if (!ready) | | &&
|| | Logical AND.
Logical OR. | if (ready && clear)
if (ready || clear) |


Converting Data Types

Converting from one data type to another is sometimes useful.

The most common example is converting string input into another type, such as an integer or a date.

As a general rule, user input is treated as a string, even if the user enters a number. Therefore, numeric input must be converted to a number before it can be used in calculations.

The commonly used conversion methods are listed below:

Method Description Example
AsInt()
IsInt() Converts a string to an integer. if (myString.IsInt())
{myInt=myString.AsInt();}
AsFloat()
IsFloat() Converts a string to a float. if (myString.IsFloat())
{myFloat=myString.AsFloat();}
AsDecimal()
IsDecimal() Converts a string to a decimal. if (myString.IsDecimal())
{myDec=myString.AsDecimal();}
AsDateTime()
IsDateTime() Converts a string to the ASP.NET DateTime type. myString="10/10/2012";
myDate=myString.AsDateTime();
AsBool()
IsBool() Converts a string to a Boolean. myString="True";
myBool=myString.AsBool();
ToString() Converts any data type to a string. myInt=1234;
myString=myInt.ToString();