Easy Tutorial
❮ Csharp Decision Csharp Struct ❯

Variables in C

A variable is simply a name given to a storage area that our programs can manipulate. In C#, each variable has a specific type, which determines the size and layout of the variable's memory. The range of values that can be stored within that memory can be stored, and the set of operations that can be applied to the variable.

We have already discussed various data types. The basic value types provided in C# can be broadly categorized as follows:

Type Examples
Integer types sbyte, byte, short, ushort, int, uint, long, ulong, and char
Floating-point types float and double
Decimal type decimal
Boolean type true or false values, as specified
Nullable types Nullable data types

C# allows the definition of other value types, such as enum, and reference types, such as class. These will be discussed in later chapters. In this chapter, we will focus on the basic variable types.

Variable Definition in C

The syntax for variable definition in C# is:

<data_type> <variable_list>;

Here, data_type must be a valid C# data type, which can be char, int, float, double, or any user-defined data type. variable_list can consist of one or more identifier names separated by commas.

Some valid variable definitions are shown below:

int i, j, k;
char c, ch;
float f, salary;
double d;

You can initialize variables at the time of definition:

int i = 100;

Variable Initialization in C

Variables are initialized (assigned a value) with an equal sign followed by a constant expression. The general form of initialization is:

variable_name = value;

Variables can be initialized at the time of declaration (given an initial value). Initialization consists of an equal sign followed by a constant expression, as shown below:

<data_type> <variable_name> = value;

Some examples:

int d = 3, f = 5;    /* Initializes d and f. */
byte z = 22;         /* Initializes z. */
double pi = 3.14159; /* Declares an approximation of pi. */
char x = 'x';        /* The variable x has the value 'x'. */

Properly initializing variables is a good programming practice, as it can sometimes lead to unexpected results if not done correctly.

Consider the following example, which uses various types of variables:

Example

namespace VariableDefinition
{
    class Program
    {
        static void Main(string[] args)
        {
            short a;
            int b;
            double c;

            /* Actual initialization */
            a = 10;
            b = 20;
            c = a + b;
            Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following result:

a = 10, b = 20, c = 30

Accepting Values from User

For example:

int num;
num = Convert.ToInt32(Console.ReadLine());

The Convert.ToInt32() function converts the data entered by the user to int data type, as Console.ReadLine() only accepts data in string format.

Lvalues and Rvalues in C

There are two kinds of expressions in C#:

Variables are lvalues and can appear on the left-hand side of an assignment. Numeric values are rvalues and cannot be assigned and cannot appear on the left-hand side. Here is a valid statement:

int g = 20;

The following statement is invalid and will generate a compile-time error:

10 = 20;
❮ Csharp Decision Csharp Struct ❯