-->

08 April 2020

C# Variables

  Asp.Net CS By Example       08 April 2020
 C# Variables 

 In this post, we are learning more about the Variable.

What Is a Variable?
 A typical program uses various values that change during its execution. For example, We a programmer create a program source code, he or she does not know what values will be enter as input by user, and that makes it necessary to process all possible values a user may enter.
 When a user enter a value that will be used in the calculation process, that value we can store temporarily on memory of computer. This value may be change throughout execution of program. For this has led or said to their name - Variables.

 Variable is a memory name space name that value may be change during program execution.Also we say A variable is a container of information, which can change its value. It provides means for:
  - storing information;
  - retrieving the stored information;
  - modifying the stored information.

 In C# programming, we are use variables to store and process information all the time.

Characteristics of Variables
 Variables are characterized by:
  - Name (identifier), for example age;
  - Type (of the information preserved in them), for example int;
  - Value (Literals,stored information), for example 25.

C# Variable Characteristics

 A variable is a named area of memory, which stores a value from a particular data type, and that area of memory is accessible in the program by its name. Variables can be stored directly in the operational memory of the program (in the stack) or in the dynamic memory in which larger objects are stored (such as character strings and arrays).

Primitive data types (numbers, char, bool) are called value types because they store their value directly in the program stack. Reference data types (such as strings, objects and arrays) are an address, pointing to the dynamic memory where their value is stored. They can be dynamically allocated and released i.e. their size is not fixed in advance contrary to the case of value types.

 Naming Variables – Rules

 When we want the compiler to allocate a memory area for some information which is used in our program we must provide a name for it. It works like an identifier and allows referring to the relevant memory area.

 The name of the variable can be any of our choice but must follow certain rules defined in the C# language specification:
  - Variable names can contain the letters a-z, A-Z, the digits 0-9 as well as the character '_'.
  - Variable names cannot start with a digit.
  - Variable names cannot coincide with a keyword of the C# language.

 For example, base, char, default, int, object, this, null and many others cannot be used as variable names. A list of the C# keywords can be found in the section "Keywords" in chapter "Introduction to Programming". If we want to name a variable like a keyword, we can add a prefix to the name – "@". For example, @char and @null are valid variable names while char and null are invalid.

 Naming Variables – Examples
 Proper names:
  - name
  - first_Name
  - _name1
 Improper names (will lead to compilation error):
  - 1 (digit)
  - if (keyword)
  - 1name (starts with a digit)

Declaring Variable
  When we declare a variable, we perform the following steps:
  - specify its type (such as int);
  - specify its name (identifier, such as age);
  - optionally specify initial value (such as 25) but this is not obligatory.

 The syntax for declaring variables in C# is as follows:

 <data type> <identifier> = [= <initialization>]; 

 Here is an example of declaring variables:
  string Name;
  int Age;

Assigning a Value
   Assigning a value to a variable is the act of providing a value that must be stored in the variable. This operation is performed by the assignment operator "=". On the left side of the operator we put the variable name and on the right side – its new value.
  Here is an example of assigning values to variables:
  Name = "Ram";
  Age = 25;

Initialization of Variable
  The word initialization in programming means specifying an initial value. When setting value to variables at the time of their declaration we actually initialize them.

Default Variable Values
  Each data type in C# has a default value (default initialization) which is used when there is no explicitly set value for a given variable. We can use the following table to see the default values of the types, which we may be already got familiar with this.

Data Type Default Value
sbyte 0
sbyte 0
byte 0
short 0
ushort 0
int 0
uint 0u
long 0l
ulong 0ul
float 0.0f
double 0.0d
decimal 0.0m
bool false
char '\u0000'
string null
object null

 Let's us summarize how to declare variables, initialize them and assign values to them with the following example.

 // Declare and initialize some variables
 bool isFound = false; 
 char Gender = 'M';
 byte Age = 28;
 short salary = 20000;
 int i = 300000; 
 byte centuries = 20;
 ushort years = 2000;
 decimal decimalPI = 3.141592653589793238m;
 bool isEmpty = true;
 char ch = 'a';
 string firstName = "Ram";
 ch = (char)5;
 char secondChar;
 // Here we use an already initialized variable and reassign it
 secondChar = ch; 
logoblog

Thanks for reading C# Variables

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.