Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 40 / 68

Shallow copies versus deep copies

This lesson covers the difference between shallow and deep copies in VB.NET, with the aim of avoiding bugs caused by unintentionally shared data.

Assigning a class instance to another variable usually copies the reference (the address) rather than the thing itself. That is a shallow copy, and changing one affects the other.

In the sample code, Dim shallow As Address = original makes shallow point at the same instance. Changing it with shallow.City = "Osaka" therefore changes what original.City displays too.

A common early stumble is thinking you have copied something when in fact both names share one instance. When you want genuinely separate data you have to make a deep copy — a new instance with the values copied across.

In professional work, understanding the difference matters for avoiding bugs where data is shared unintentionally — copying a template of settings and then editing it individually, for instance.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)