C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
So far, we have discussed the
multidimensional arrays in C#. Now let's see about variables and some memory elements of C# in this chapter.
Objective
The main objective of this article is to learn about variables, stack, heap, definite assignment and default values of
C# programming.
Variables
Variable represents a storage location of the memory that contains a modifiable value (data which can be changeable).
It can be a parameter
(value, out or ref), local variable
(described locally), field
(static or instance), or an array element
.
Stack and Heap
Stack and Heap are the important parts of the memory where variables
reside in.
Stack:
Heap:
Note: We cannot delete the objects
in C# explicitly as we do in C++.
Definite Assignment
C# enforces the definite assignment policy. This means, it is impossible to access the unallocated (or uninitialized) memory from the outside.
Definite assignment policy has three implications, which are,
- Local variables must have been assigned a value before they can be read.
- When a method is called, Function arguments must be supplied.
- Other elements such as array elements and fields are automatically initialized by the runtime.
Default Values
In
C#, all the type instances have the default values which results as bitwise zeroing of the memory.
- For all reference types -
null
- For all numeric and enum types -
0
- For bool type -
false
- For char type - '
\0
'
In order to obtain the default value for any type, we have to use the
default
keyword.
Conclusion
In this article, we have seen what is variable and memory elements of C#. Hope you understood the article.
Thanks for reading.
Regards,
Krishna.