Skip to main content

Command Palette

Search for a command to run...

Variables and data types in C#

An introduction to data types

Updated
3 min read
Variables and data types in C#
D

I’m a Cloud Engineer specializing in Microsoft Azure with 15 years of experience as a sysadmin in Linux and network administration.

💻 I work at Condor Comunicaciones / Nova Cloud as Project Manager and Cloud Solutions Manager and I’m certified as Azure Solutions Architect.

  • 🔭 I’m currently working on all things Azure
  • 🌱 I’m currently learning C#, PowerShell and Terraform
  • 💬 Ask me about Cloud Computing

In C#, there are built-in types (e.g., integers, floating-point numbers, characters, and Booleans) and user-defined (custom) types (e.g., classes, structs, and enums).

Data types specify the kind of data a variable can hold or the kind of values an object can represent. Types are important because they determine the operations that can be performed on the data, the way data is stored in memory, and how it is passed between methods.

A data type defines the kind of values a variable can hold and the operations that can be performed on those values. In other words, a data type is a blueprint, while a variable is a storage location that follows that blueprint.

Relationship between data types and variables

When we declare a variable, we must specify its data type.

This informs the compiler about the kind of data the variable can store and the operations that can be applied to it. In this case, integer values and you can perform operations like addition, subtraction, multiplication, and division on it.

The data type of a variable determines how much memory is allocated for that variable and how the data is interpreted when it's read or modified.

Data types

In C# and other programming languages, a variable of a value type contains an instance of the type. This differs from a variable of a reference type, which contains a reference to an instance of the type.

There are built-in types included in C# that we can use and serve as the basis for creating more complex custom types.

  1. Value Type:
  • Value types directly contain their data, and memory is allocated on the stack.

  • When a value type variable is assigned to another variable, a copy of the value is created. Therefore, changes to one variable do not affect the other.

  • Value types are usually more efficient in terms of memory usage and access speed.

  1. Reference Type:
  • Reference types store a reference (memory address) to the memory location where the data is stored, rather than the data itself. Memory for reference types is allocated on the heap.

  • When a reference type variable is assigned to another variable, both variables refer to the same memory location. Therefore, changes made to one variable also affect the other.

Here's an example:

// *** Value type ***
int a = 10;
int b = a; // We assign the *value* of a to b
b += 5; // We increment the b variable only

Console.WriteLine($"a: {a}, b: {b}"); 
// Output: a: 10, b: 15 

// ***  Reference type ***
// we create a new object of the class StringBuilder
StringBuilder sb1 = new StringBuilder("Hello");
StringBuilder sb2 = sb1; // we assign sb1 to sb2
sb2.Append(", World!"); // we add text to the original string

Console.WriteLine($"sb1: {sb1}, sb2: {sb2}"); 
// Output: sb1: Hello, World!, sb2: Hello, World!

In this example, when we assign a to b, a new copy of the value is created. Thus, modifying b does not affect a. However, when we assign sb1 to sb2, both variables reference the same memory location, so changes made to one variable are visible to the other.

Summary of Data types

Here's a summary of the different kind of value types and reference types available in C#.

Conclusion

In conclusion, C# offers various data types that help manage how data is stored, accessed, and manipulated. Understanding the differences between value types and reference types, as well as built-in and custom types, is essential for efficient programming in C#.

Reference

C# Concepts

Part 1 of 1

Concepts and C# basics I come across my learning journey.