Working with Variables

In this chapter you will learn:

  • How to create variable in C#?
  • How to assign value in a variable in runtime?
  • How to convert value according to types of variable?

A variable refers to the memory address. When you create variable, it creates holds space in the memory that is used for storing temporary data. As you know about c# data types, each data type has predefined size. In this chapter you will learn how to use data types to create variable.

  • Output

    Please enter first value
    Enter second Value
    Add of 5 and 7 is 12

    In the preceding example we create three integer type variable num1,num2 and result. num1 and num2 is used for accepting user value and result is used for adding both number. The new thing in the preceding example is number of placeholders.


    If you want to display more than one variable values then you will have to assign place holder for each variables. In the preceding line {0} denotes to num1, {1} denotes to num2 and {2} denotes to result.


    C# accepts string value by default. If you are using other value then you will have to convert of specific data types.


    You can use the following method to convert one data type to another data type.



    In this chapter, you learned about how to use variables in C# and how to assign value at runtime. You also learned about how to convert value according to types of variables. Next is example section, in which you will see some  programming examples of C# data types and variables.

    4 thoughts on “Working with Variables”

      1. int and Int32 are synonymous,int represent 32 bit memory as same as Int32 but latter one shows explicitly 32-bit to those who are reading your code.

      2. “int” Data type means Int 32 bit only. “short” Data type means Int 16 bit, and “long” Data type means Int 64 bit only.

    Leave a Reply

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