ASP.NET Razor - C# Variables
- Previous Page Razor Syntax
- Next Page Razor C# Loop
Variables are named entities used to store data.
Variables
Variables are used to store data.
Variable names must start with a letter character and cannot contain spaces or reserved characters.
Variables can be a specific type, indicating the data type of the data it stores. String variables store string values ("Welcome to CodeW3C.com"), integer variables store numeric values (103), date variables store date values, and so on.
Declare variables using the var keyword or type, but ASP.NET usually can automatically determine the data type.
Example
// Use var keyword: var greeting = "Welcome to CodeW3C.com"; var counter = 103; var today = DateTime.Today; // Use data type: string greeting = "Welcome to CodeW3C.com"; int counter = 103; DateTime today = DateTime.Today;
Data type
Here is a list of common data types:
Type | Description | Example |
---|---|---|
int | Integers | 103, 12, 5168 |
float | Floating-point numbers | 3.14, 3.4e38 |
decimal | Decimal | 1037.196543 |
bool | Logical values | true, false |
string | String values | "Hello CodeW3C.com", "Bill" |
Operators
Operators inform ASP.NET which type of command to execute in an expression.
C# language supports various operators. Here are some common operators:
Operators | Description | Example |
---|---|---|
= | Assign a value to a variable. | i=6 |
|
|
|
|
|
|
== | Equal. Returns true if the values are equal. | if (i==10) |
!= | Not equal. Returns true if the values are not equal. | if (i!=10) |
|
|
|
+ | String concatenation (chaining or juxtaposition) | "w3" + "school" |
. | Dot. Separate objects and methods. | DateTime.Hour |
() | Parentheses. Group values. | (i+5) |
() | Parentheses. Pass parameters. | x=Add(i,5) |
[] | Parentheses. Access values in an array or collection. | name[3] |
! | Not. Inverts true or false. | if (!ready) |
|
|
|
Convert Data Types
Converting one data type to another can be very useful sometimes.
Most common examples include converting string input to another type, such as an integer or a date.
As a rule, user input will be treated as a string, even if the input is a number. Therefore, numeric input values must be converted to numbers before they are used for calculations.
Below is a list of commonly used conversion methods:
Method | Description | Example |
---|---|---|
|
Convert a string to an integer. |
if (myString.IsInt()) {myInt = myString.AsInt();} |
|
Convert a string to a floating-point number. |
if (myString.IsFloat()) {myFloat = myString.AsFloat();} |
|
Convert a string to a decimal number. |
if (myString.IsDecimal()) {myDec = myString.AsDecimal();} |
|
Convert a string to an ASP.NET DateTime type |
myString = "10/10/2012"; myDate = myString.AsDateTime(); |
|
Convert a string to a logical value. |
myString = "True"; myBool = myString.AsBool(); |
ToString() | Convert any data type to a string. |
myInt = 1234; myString = myInt.ToString(); |
- Previous Page Razor Syntax
- Next Page Razor C# Loop