Value Types

Value types are used for a single piece of information which is stored in an area called the Stack
This is a fixed amount of memory.
All the intrinsic types are value types, for example integers

There are two fundamental building blocks of .NET types. The class and the value type
Assigning one value type variable to another copies the contained value.

These are passed by value and a copy of the variable is made.
User defined structs are value types

Classes are known as reference types because you always access objects by using reference variables.
A reference variable lets us refer to an object created by the "New" operator.
Accessing objects in this way allows the .NET garbage collection mechanism to reclaim the resources used by an object when no body has a reference to it anymore.
This feature makes for efficient memory usage and eliminates the possibility of memory leaks

All operations on objects are done by calling member functions, using the -> operator.
Value types hold values (integers, floating point numbers etc), anything that is basically a wrapper around a simple value and is less than about 16 bytes in size.
We need value types as we want simple values to be used as efficiently as possible.

.NET gets around this problem by using Value types. These value types are represented and used efficiently as built-in types, which can also be used as objects when necessary (often refered to as boxing)
There are performance issues to consider when using reference types. Reference types have to be accessed via their pointers, affecting both the size and speed of the compiled application.

A Value type is a type that inherits from the System::ValueType class. Value types have several special properties:

  • Value types are stored on the stack (unlike references which are stored on the run-time heap)

  • Instances of value types are always accessed directly (unlike reference types which are accessed through references) This means that you don't use the "New" operator whne creating instances

  • Copying value types copies the value rather than the reference

The important difference between structures and classes is that structures are value types. This means that if you have a value type that needs to have some internal structure you can implement is using a "Struct"

Integers range from four to eight bytes
You can know loop through 1 to 65,000 rows in Excel using an Integer

The Currency value type (previously eight bytes) has been replaced with the more capable Decimal (12 bytes)
This is used when you have large numbers that you do not want rounded internally (which single and double do)

The Value Variant type in VBA has been replaced with the Object reference type

© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext