Tuesday, May 3, 2016

CSharp Variables

What are variables in general


In any programming language, a variable refers to a space in the computers memory. We give this space a meaningful name so that we can refer to that memory in our code easily without causing any confusion. 




Assigning a value to a variable means that we intend to put a value in the memory identified by the variable.


How to declare variables in C#


The syntax in C# to declare a variable is to first write the type and then its name like so:


typeOfVariable nameOfVariable;

            int counter;
            string name;
            double price;



Declare and Assign a value at the same time



You can declare and assign the value to a variable in a signle step like so:

typeOfVariable nameOfVariable = thevalueofthevariable;

These are some good examples of variable declarations with value assignment.

            int counter = 0;
            string name = "Your Name";
            double price = 24.99d;

These are some bad examples of variable as their names do seem to be meaningful:
            int x = 0;
            string theVariable = "Your Name";
            double p = 24.99d;

No comments:

Post a Comment