.netASP.Net Razor

ASP.net Razor Variables,Datatypes and Operators

Variables are used to store data.

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

A variable can be of a specific type, indicating the kind of data it stores. String variables store string values (“Welcome to Techkatak”), integer variables store number values (100), date variables store date values, etc.

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


Data Types:
Below is a list of common data types:

int:Integer (whole numbers) 100, 20, 6158
float:Floating-point number 3.14, 3.4e38
decimal:Decimal number (higher precision)Ex: 2139.196543
bool:Boolean Ex: true, false
string:String Ex: “Hello Techkatak”, “Admin”

An operator tells ASP.NET what kind of command to perform in an expression.

The C# language supports many operators. Below is a list of common operators:

Operators with Examples:

= Assigns a value to a variable. i=6

  • ,- ,* ,/ : Adds/Subtracts/Multiplies/Divides a value or variable.

+=,-=: Increments/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,Greater than or equal.
if (i<10)
if (i>10)
if (i<=10)
if (i>=10)

  • : Adding strings (concatenation). “Welcome” + “TechKatak”

. Dot: Separate objects and methods. DateTime.Hour

() Parenthesis: Groups values. (i+5)

() Parenthesis: Passes parameters. x=Add(i,5)

[] Brackets: Accesses values in arrays or collections. name[3]

! Not: Reverses true or false. if (!ready)

&&: Logical AND.if (ready && clear)

||: Logical OR. if (ready || clear)

Converting Data Types:
Converting from one data type to another is sometimes useful.

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

As a rule, user input comes as strings, even if the user entered a number. Therefore, numeric input values must be converted to numbers before they can be used in calculations.

List of common conversion methods with Description:

AsInt(),IsInt(): Converts a string to an integer.
if (myString.IsInt())

AsFloat(),IsFloat():Converts a string to a floating-point number.

if (myString.IsFloat())

AsDecimal(),IsDecimal():Converts a string to a decimal number.

if (myString.IsDecimal())

AsDateTime(),IsDateTime(): Converts a string to an ASP.NET DateTime type.


AsBool(),IsBool():Converts a string to a Boolean.


ToString():Converts any data type to a string.

© 2015, www.techkatak.com. All rights reserved.