C#

C# – Variable

A typical program uses various values that change during its execution. For example, we create a program that performs some calculations on the values entered by the user. The values entered by one user will obviously be different from those entered in by another user. This means that when creating the program, the programmer does not know what values will be introduced as input, and that makes it necessary to process all possible values a user may enter.

When a user enters a new value that will be used in the process of calculation, we can preserve it (temporarily) in the random access memory of our computer. The values in this part of memory change (vary) throughout execution and this has led to their name – variables.

C# defines several different kinds of variables. The kind that we have been using are called local variables because they are declared within a method.

Initializing a Variable

One way to give a variable a value is through an assignment statement, as you have already seen. Another way is by giving it an initial value when it is declared. To do this, follow the variable’s name with an equal sign and the value being assigned. The general form of initialization is shown here: type var-name = value; Here, value is the value that is given to the variable when it is created. The value must be compatible with the specified type. Here are some examples:


int count = 10; // give count an initial value of 10

char ch = 'X'; // initialize ch with the letter X

float f = 1.2F; // f is initialized with 1.2

Dynamic Initialization

Although the preceding examples have used only constants as initializers, C# allows variables to be initialized dynamically, using any expression valid at the point at which the variable is declared.

Implicitly Typed Variables

As explained, in C# all variables must be declared. Normally, a declaration includes the type of the variable, such as int or bool, followed by the name of the variable. However, beginning with C# 3.0, it became possible to let the compiler determine the type of a local variable based on the value used to initialize it. This is called an implicitly typed variable. An implicitly typed variable is declared using the keyword var, and it must be initialized. The compiler uses the type of the initializer to determine the type of the variable. Here is an example:

var e = 2.7183;

Because e is initialized with a floating-point literal (whose type is double by default), the type of e is double. Had e been declared like this:

var e = 2.7183F;

then e would have the type float, instead.

Example

using System;

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

Lvalue and Rvalue Expressions in C#

There are two kinds of expressions in C# −

  • lvalue − An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.
  • rvalue − An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

Variables are lvalues and hence they may appear on the left-hand side of an assignment. Numeric literals are rvalues and hence they may not be assigned and can not appear on the left-hand side. Following is a valid C# statement −

int g = 20;

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Check Also
Close
Back to top button