Choosing the type of variable

In Chapter 2Understanding Classes, Structures, and Interfaces, in the Data types in C# section, we saw the different data types that are possible for both value and reference types. We also did a code implementation to see the difference in the behavior of Struct, which is a value type, and Class, which is a reference type. In this section, we will do a deep dive and see this difference in behavior and how it can help us to choose the correct type for our variable.

Let's analyze how the following code statements for value and reference types and see how they differ in implementation:

// Value Type
int x = 10;
int y = x

// Reference Type
Car c = new Car();
Car c2 = c;

In the preceding code, we have declared the value type variables x and y. While declaring, the x variable has been assigned a value. In the next step, we are assigning x to y. Similarly, we have a class named Class and we have created an object of c. In the next statement, we have declared another object of the same class and have assigned c to c2

Please refer to the following diagram, which shows how these types are implemented and managed inside memory:

In the preceding diagram, we have declared variable x as an int data type and c as an object of the Car class. Now, we know that int is a value type, while Class is a reference type. So let's try to analyze why the behavior differs for both of them:

  • For x, in the first statement, that is, int x = 10, a block of memory is reserved by the application. The rectangular block below the declaration conveys that.
  • Now, when we execute the int y = x statement, we are declaring another variable, y, and it is assigned the value currently in x. What it does internally is it allocates another block of memory for y in memory. Therefore, as x and y are not pointing to the same memory location, they will hold different values.
  • On the other hand, if we look at the Car class, we have just declared two attributes in it: the registration number and color. Now, when we use the new statement, what it does is that it creates an object for the class and allocates it memory. However, as against the value type implementation, it does not save the value in the object. Instead, in the object, it just saves a reference to the allocated memory block. In the rectangular shape in the preceding diagram, you will see that, once the c object is created for the Car class, a pointer is saved in the created object.
  • Now, when we execute the Car c2 = c; statement, internally, it creates a new object, c2, but does not allocate a new memory block for the object. Instead, it just saves a reference to the memory location shared with the object, c

As illustrated by the preceding implementation, whenever a new value type variable is declared, a new block of memory is reserved by the application, which is different from reference type variables.

Hence, in much simpler terms, the following factors can help us to choose between value and reference type:

  • A value type variable is logically immutable: In very simple terms, it means that on every declaration of the value type, a new block of memory is reserved by the application. As they are different memory allocations, it implies that if we execute any operation on one memory location, the change is not transmitted across to the other memory location.
  • Whether there are lots of objects: If there lots of objects being created in the application, it might be better to not create them as value type as it would exponentially increase the memory requirements of the application.
  • Whether the object is small: If the object is small, then it may make sense to have them as value type variables. However, if we think that the object is bound to have too many properties, a reference type variable will make more sense.
  • Memory management: Value type variables are managed on a stack whereas reference type variables are managed on a heap. When we move to Chapter 9, Manage the Object Life Cycle, we will look further into memory management and how the garbage collector works.

Now that we have a fair understanding of how we can create and consume different data types in a C# application, we will be looking at some of the features of C# that help us to set correct behavior for the different types we use in the application. In the next section, we will be looking at static variables and how they are implemented in C#.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.129.194