Using Variables

0
95
views
Variables
Using Variables

Consider a situation where you have to create a program that accepts two numbers from a user and displays the sum of the numbers on the screen.

Now, while reading the numbers provided by the user, you need to store these numbers somewhere in the memory so that you can perform the add operation on the numbers. You can store the numbers in the memory by using variables.

The following figure shows the process of storing user input in the memory by using variables.

Variables
Using Variables to Store User Input

In the preceding figure, a user provides 5 and 3 as input. The digit 5 is stored in a variable, Num1 and digit 3 is stored in a variable, Num2. A variable is a location in the memory that has a name and contains the value.

The value could be an integer, such as 27, a decimal, such as 9.85, or
a character, such as ‘L’. A variable is associated with a data type that defines the type of data that can be stored in the variable.

For example, a variable called TennisPlayerName will ideally store characters, whereas a variable called High_Score will store numbers. A program refers to a variable by its name.

Naming Variables in C#

In C#, the following rules are used for naming variables:

A variable name must begin with a letter or an underscore (‘_’), which may be followed by a sequence of letters, digits (0-9), or underscores.
The first character in a variable name cannot be a digit.

A variable name should not contain any embedded spaces or symbols, such as ? ! @ #

  • A variable name must begin with a letter or an underscore (‘_’), which may be followed by a sequence of letters, digits (0-9), or underscores. The first character in a variable name cannot be a digit.
  • A variable name should not contain any embedded spaces or symbols, such as ? ! @ # – % ^ & * ( ) [ ] { } . , ; : ” ‘ / and . However, an underscore can be used wherever space is required, like High_Score.
  • A variable name must be unique. For example, to store four different numbers, four unique variable names need to be used.
  • A variable name can have any number of characters.
  • Keywords cannot be used as variable names. For example, you cannot declare a variable named the class as it is a keyword in C#.

Some examples of valid variable names are:

  • Game_level
  • High_score123
  • This_variable_name_is_very_long

Some examples of invalid variable names are:

  • #Score
  • 2strank

Declaring and Initializing Variables

You can declare and initialize variables by using the following syntax:

In the preceding syntax, the represents the kind of data type that will be stored in a variable and specifies the value that needs to be stored in the
variable.

Consider the following statement that declares a variable: int age = 1;

The preceding statement declares a variable named age of the int data type. In addition, the statement initializes the variable with the value, 1. The int data type is used to store numeric data (integers).

Consider the following statement: char choice=‘y’;

The preceding statement declares the variable choice of the char data type and initializes the variable with the value, y.

Previous articleC# Classes
SHARE

LEAVE A REPLY

Please enter your comment!
Please enter your name here