Latest web development tutorials

ASP.NET Razor C # variables

Variable is a named entity used to store data.


variable

Variable is used to store data.

A variable name must begin with an alphabetic character, and can not contain spaces or reserved characters.

A variable can be a specified type, it indicates the type of data it stores. string variable to store the string value ( "Welcome to W3CSchool.cc"), integer variable stores the digital value (103), date variable to store date values, and so on.

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

Examples

// Using the var keyword:
var greeting = "Welcome to W3CSchool.cc";
var counter = 103;
var today = DateTime.Today;

// Using data types:
string greeting = "Welcome to W3CSchool.cc";
int counter = 103;
DateTime today = DateTime.Today;


type of data

Listed below are commonly used data types:

类型 描述 实例
int 整数(全数字) 103, 12, 5168
float 浮点数 3.14, 3.4e38
decimal 十进制数字(高精度) 1037.196543
bool 布尔值 true, false
string 字符串 "Hello W3CSchool.cc", "John"


Operators

Operators tell ASP.NET what kind of command in the expression.

C # language supports a variety of operators. Listed below are commonly used operators:

运算符 描述 实例
= 给一个变量赋值。 i=6
+
-
*
/
加上一个值或者一个变量。
减去一个值或者一个变量。
乘以一个值或者一个变量。
除以一个值或者一个变量。
i=5+5
i=5-5
i=5*5
i=5/5
+=
-=
变量递增。
变量递减。
i += 1
i -= 1
== 相等。如果值相等则返回 true。 if (i==10)
!= 不等。如果值不等则返回 true。 if (i!=10)
<
>
<=
>=
小于。
大于。
小于等于。
大于等于。
if (i<10)
if (i>10)
if (i<=10)
if (i>=10)
+ 连接字符串(一系列互相关联的事物)。 "w3" + "schools"
. 点号。分隔对象和方法。 DateTime.Hour
() 圆括号。将值进行分组。 (i+5)
() 圆括号。传递参数。 x=Add(i,5)
[] 中括号。访问数组或者集合的值。 name[3]
! 非。真/假取反。 if (!ready)
&&
||
逻辑与。
逻辑或。
if (ready && clear)
if (ready || clear)


Converting Data Types

Conversion from one data type to another data type, sometimes very useful.

The most common example is to convert the input string to another type, such as integer or date.

Under the general rule, it is seen as the user input string processing, even if the user enters the number. Therefore, the value must be converted to a digital input, before it can be used for calculations.

The following lists common conversion methods:

方法 描述 实例
AsInt()
IsInt()
转换字符串为整数。 if (myString.IsInt())
{myInt=myString.AsInt();}
AsFloat()
IsFloat()
转换字符串为浮点数。 if (myString.IsFloat())
{myFloat=myString.AsFloat();}
AsDecimal()
IsDecimal()
转换字符串为十进制数。 if (myString.IsDecimal())
{myDec=myString.AsDecimal();}
AsDateTime()
IsDateTime()
转换字符串为 ASP.NET DateTime 类型。 myString="10/10/2012";
myDate=myString.AsDateTime();
AsBool()
IsBool()
转换字符串为布尔值。 myString="True";
myBool=myString.AsBool();
ToString() 转换任何数据类型为字符串。 myInt=1234;
myString=myInt.ToString();