Razor Tutorial – How to Use Variables and Datatypes

In this chapter you will learn

  • How to define Variables with DataTypes in Razor Markup
  • Converting Datatypes
  • Programming Example

How to define Variables and DataTypes in Razor Markup

There are two types of variable initialization in Razor Markup.

  1. Implicitly Typed Variable : Using var keyword you can initialize any type of variable in razor syntax. It is known as Implicitly-typed variable. You must initialize the variable with value if it is var variable.
    Example:
  2. Explicitly Typed Variable : This variable is defined with data typed as int, string, float etc.

Programming Example

Output

Add Two Numbers
5 + 5 = 10
Using Explicit Data Types
10 x 10 = 100
  • DataTypes

    You can use all the C# or VB data types in Razor Syntax as the way you use in programming. But the process of converting data type is different and you must know the concept of conversion in Razor markup.

    Converting Data Types

    1. Converting Integer – Convert string that has value like integer, example “239” or “45”. There are two methods for converting int. IsInt() Method and AsInt() Method.

    IsInt() – It checks whether provided variable has integer value or not.
    AsInt() – It converts variable into integer datatypes.

    Example Converting Integer

    Output

    525 is Integer Value
    2. Converting Bool – Converts string “true” and “false” into Boolean type.
    There are two methods for converting Bool. IsBool() Method and AsBool() Method.

    IsBool() – It checks whether provided variable has Boolean value or not.
    AsBool() – It converts variable into Boolean datatypes.

    Example Converting Bool

    Output

    False
    3. Converting Float – Converts a string that has a decimal value like “2.5” or “6.123”. There are two methods for converting Float. IsFloat() Method and AsFloat() Method.

    IsFloat() – It checks whether provided variable has Float value or not.
    AsFloat() – It converts variable into Float datatypes.

    Example Converting Float

    Output

    2.63
    4. Converting Decimal – Converts string that has a decimal value like “2.6” or “6.1”. There are two methods for converting Decimal. IsDecimal() Method and AsDecimal() Method.

    IsDecimal() – It checks whether provided variable has Decimal value or not.
    AsDecimal() – It converts variable into Decimal datatypes.

    Example

    Output

    2.63
    5. Converting DateTime – Converts string that has value in date time format. There are two methods for converting DateTime. IsDateTime() Method and AsDateTime() Method.

    IsDateTime() – It checks whether provided variable has DateTime value or not.
    AsDateTime() – It converts variable into DateTime datatypes.

    Example

    Output

    Date is : 24-08-2016 AM 12:00:00
    6. Converting StringToString() method converts any data type into string.

    Example

    Output

    2342 is String Now.
    24-08-2016 PM 08:21:00 is String Now.
    True is String Now.

    Summary

    In this chapter you learned to define variable and converting data types in Razor Syntax. In the next chapter you will learn to use Operators in Razor Syntax.


    Leave a Reply

    Your email address will not be published. Required fields are marked *