Hello!

This is a new blog post in the PowerShell to C# and back series, I hope you’ll like it!

Introduction

A variable is a memory location that holds arbitrary information for later use.

In simpler terms, it is a temporary storage container for you to put data and get data out whenever it is required using a unique name identifier.

Variables can store data or data types (value or reference), so variables containing value types directly contain their data, whereas variables containing reference type store references to their data, this is true for both C# and PowerShell.

Before we begin… the following is a shameless plug of my new book 😎 👇


[Announcement] PowerShell to C# and back Book

This book bridges the concept and knowledge gap between a scripting language like PowerShell and a modern programming language like C#, which is a natural language of choice for People who know PowerShell.

An increase in adoption of software development inside Infrastructure teams makes this the perfect time to get started with a programming language and C# is a natural choice for people using PowerShell today. As it fits best with the basket of matching skillsets like Azure, Azure DevOps which will add value to your career

Download the FREE Book Sample from the book web page which covers all the basics of C# within the first 50 pages 👉 https://leanpub.com/powershell-to-csharp



Value Types

A value type variable is immutable data stored in a memory area known as a stack. Data in the stack is short-lived and some common data types that are stored in the stack are Struct, Int32, Double etc.

The following image demonstrates that when a value type variable is declared and data is stored. But when we attempt to assign a value type variable to another variable then data stored in the first variable is copied to the second variable.


Reference Types

A reference type variable holds a reference to data stored in a memory area known as the heap. Heap is long-lived compared to stack memory. More than one variable can point to the same referenced data. Data types such as Class, Delegates and String are stored as a reference data type.

The following images demonstrate how an object is saved as a reference variable type and when the first variable is assigned to the second variable, then the reference to the same value is assigned to the second variable.

In the above C# example, both variables X and Y point to the same Book object in memory. That means if we change X then the value of Y will also change. In other words X and Y don’t store any data but reference to the same memory location.

In PowerShell to define a reference type variable, we use [ref] and you can learn more about it by running the command Get-Help About_ref. Like in the following example we have created a $referenceVar that stores reference to a memory location. That is why when the $referenceVar is incremented in the increment() function scope the value at the referenced memory location increments by 1 every time the function is called. Read more here.

 

 

Optical Character Recognition

Author of “PowerShell Guide to Python“, “Windows Subsystem for Linux (WSL)“, “Learn C# in 30 minutes” and currently writing the most awaited book: “PowerShell to C# and Back“!


Subscribe to our mailing list

* indicates required