ASP.NET Razor - C# Variables


Variables are named entities used to store data.


Variables

Variables are used to store data.

A variable's name must begin with an alphabetic character and cannot contain spaces or reserved characters.

A variable can be a specific type, representing the type of data it stores. string variables store string values ("Welcome to example.com"), integer variables store numeric values (103), date variables store date values, and so on.

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

Examples

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

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


Data Types

The following lists commonly used data types:

Type Description Example
int Integer (whole numbers) 103, 12, 5168
float Floating-point number 3.14, 3.4e38
decimal Decimal number (high precision) 1037.196543
bool Boolean true, false
string String "Hello example.com", "John"


Operators

Operators tell ASP.NET what commands to perform in an expression.

The C# language supports many operators. Below are commonly used operators:

Operator Description Example
= Assigns a value to a variable. i=6
+
-
*
/
Adds a value or a variable.
Subtracts a value or a variable.
Multiplies a value or a variable.
Divides a value or a variable.
i=5+5
i=5-5
i=5*5
i=5/5
+=
-=
Increments a variable.
Decrements a variable.
i += 1
i -= 1
== Equality. Returns true if values are equal. if (i==10)
!= Inequality. Returns true if 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 interrelated items). "run" + "oob"
. Dot. Separates objects and methods. DateTime.Hour
() Parentheses. Groups values. (i+5)
() Parentheses. Passes parameters. x=Add(i,5)
[] Square brackets. Access values of arrays or collections. name[3]
! Not. Reverses 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 to another type, such as an integer or a date.

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

The following lists commonly used conversion methods:

Method Description Example
AsInt()
IsInt()
Converts a string to an integer. if (myString.IsInt())
{myInt=myString.AsInt();}
AsFloat()
IsFloat()
Converts a string to a floating-point number. if (myString.IsFloat())
{myFloat=myString.AsFloat();}
AsDecimal()
IsDecimal()
Converts a string to a decimal number. if (myString.IsDecimal())
{myDec=myString.AsDecimal();}
AsDateTime()
IsDateTime()
Converts a string to an 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();

Other Extensions