Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET the Razor C# variable


May 12, 2021 ASP.NET


Table of contents


ASP.NET Razor - C# variable

In ASP.NET razor supports C sharp in this section, which explains the variables of C.

Variables are named entities that are used to store data.


Variable

Variables are used to store data.

The name of a variable must begin with a letter character and cannot contain spaces or reserve characters.

A variable can be a specified type that represents the type of data it stores. The string variable stores string values ("Welcome w3cschool.cn"), the integer variable stores the numeric value (103), the date variable stores the 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 usually automatically determines the data type.

// 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;


The data type

Common data types are listed below:

type describe Example
int Integer (full number) 103, 12, 5168
float Floating point number 3.14, 3.4e38
decimal Decimal number (high precision) 1037.196543
bool Boolean value true, false
string String "Hello w3cschool.cn", "John"


Operator

The operator tells the ASP.NET what kind of commands are executed in the expression.

The C# language supports a variety of operators. Common operators are listed below:

Operator describe Example
= Assign a variable. i=6
+
-
*
/
Add a value or a variable.
Lower a value or a variable.
Multiply one value or a variable.
Remove with a value or a variable.
i=5+5
i=5-5
i=5*5
i=5/5
+=
-=
The variable is incremented.
Variables are decremented.
i += 1
i -= 1
== equal.Returns true if the value is equal. if (i==10)
!= Not equal.Returns true if the value is inequal. if (i!=10)
<
>
<=
>=
Smaller than.
more than the.
It is less than or equal.
greater or equal to.
if (i<10)
if (i>10)
if (i<=10)
if (i>=10)
+ Connect strings (a series of interrelated things). "w3" + "schools"
. Dot number.Separate objects and methods. DateTime.Hour
() Parentheses.Group values. (i+5)
() Parentheses.Transfer parameters. x=Add(i,5)
[] Brand brackets.Access the value of an array or a collection. name[3]
! No.Real / false. if (!ready)
&&
||
Logic and.
Logic or.
if (ready && clear)
if (ready || clear)


Transform the data type

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

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

In general, user input is treated as string processing, even if the user enters a number. The numeric input must therefore be converted to a number before it can be used for calculations.

The common conversion methods are listed below:

method describe Example
AsInt()
IsInt()
The conversion string is an integer. if (myString.IsInt())
{myInt=myString.AsInt();}
AsFloat()
IsFloat()
The conversion string is the floating point number. if (myString.IsFloat())
{myFloat=myString.AsFloat();}
AsDecimal()
IsDecimal()
The conversion string is a decimal number. if (myString.IsDecimal())
{myDec=myString.AsDecimal();}
AsDateTime()
IsDateTime()
The conversion string is the ASP.NET DATETIME type. myString="10/10/2012";
myDate=myString.AsDateTime();
AsBool()
IsBool()
The conversion string is the Boolean value. myString="True";
myBool=myString.AsBool();
ToString() Convert any data type as a string. myInt=1234;
myString=myInt.ToString();

Related articles

The variable