Detailed analysis of ASP. NET Razor C variables

  • 2021-11-13 07:09:50
  • OfStack

Variable

Variables are used to store data.

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

A variable can be of a specified type, indicating the data type it stores. The string variable stores string values ("Welcome to RUNOOB. COM"), the integer variable stores numeric values (103), the 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 a type), but ASP. NET typically determines the data type automatically.


// 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 following is a list of commonly used data types:

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

Operator

Operator tells ASP. NET what kind of command to execute in the expression.

The C # language supports a variety of operators. The following is a list of commonly used operators:

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

Convert data types

It is sometimes useful to convert from one data type to another.

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

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

The following is a list of commonly used conversion methods:

方法 描述 实例
AsInt()
IsInt()
转换字符串为整数。 if (myString.IsInt())
{myInt=myString.AsInt();}
AsFloat()
IsFloat()
转换字符串为浮点数。 if (myString.IsFloat())
{myFloat=myString.AsFloat();}
AsDecimal()
IsDecimal()
转换字符串为10进制数。 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();

The above is the detailed analysis of C # variable of ASP. NET Razor. Please pay attention to other related articles on this site for more information about C # variable of ASP. NET Razor!


Related articles: