C# Data Types in Depth

Synopsis

  1. Introduction.
  2. Definition of data type.
  3. Need of variable.
  4. Use of data type.
  5. Advantages of data type.
  6. Conclusion.

Introduction

Memory is a primary use in programs, without memory programs can't do anything. Memory is a storage space or platform of a process of a program or software. This article explains how to allocate memory, how to save memory and how to set a name for memory. Handle the memory with care because memory is not a simple thing, it is very expensive.

Definition of data type

A data type is a type of data that specifies to the compiler the type of data store in the allocated memory.

Need of variable

First create the memory then store data into the memory. If you do not create it then we cannot store data into it. If we create memory the we should assign a name for the memory. We cannot access memory physically, only logically, if it is accessed logically then we only nee to use the memory's name or its address.

A variable is the name of a memory name. There are many rules available to create a variable name.

  • Variable name should be meaningful.
  • Not allowed blank space in variable name.
  • First character should be a letter or underscore.
  • Variable name should not be reserved words.
  • The maximum variable length is 511 characters.

The following explains how to declare a variable and how to allocate memory.

Example

declare variable

Memory Allocation

Memory Allocation

Figure 1: Memory Allocation

The way to allocate memory is shown in Figure 1. "First" is a variable name. "1001" is a memory address. The system generates a memory address automatically. Memory increases based on the data type byte. For example an integer has 2 bytes so the memory address increaseas by two. For example a memory address of 1001 and next memory address is 1003. It is based on data type.

Example

data stores in its memory

Figure 2: Data stores in its memory

data stores

Figure 3: Data stores

Here declare two variables "’First, Second” and assign values. Figure 2 shows how the data is stored in memory the same as in Figure 3.

Use of data type

The data type defines the size of the memory. Store the data into memory and choose the relevant data type based on which data wil be stored. Memory is very expensive so do not waste memory. If we are going to store an integer data type then do not declare it as double because if the allocated memory is for one variable then we cannot use another variable so memory will be wasted.

memory status
Figure 4: Memory status

In Figure 4 the Red color represents used memory and the Green color represents unused memory.

Unused memory cannot be used anywhere, it can store only a specific capacity using the same variable. In "varFirst" only an integer is store that must not be declared a double because that specific memory takes more memory. If memory is allocated then we cannot use it so it is a waste of memory.

Advantages of data type

  • Data type defines the size of a variable in memory.
  • Data type saves memory from unused memory.
  • It is used to store various types of data using data type.
  • Can create a user-defined data type using standard data type.

Conclusion

Data types are at the heart of programs in C#, not only C#, but in every program. Handle the memory using a data type. Data type and variable both are very important. Programmers use the correct data type in the correct place.

Up Next
    Ebook Download
    View all
    Learn
    View all