.NET Array Dictionary List String Sub ArrayList Cast Class Console Dates DataTable DateTime Enum File For Format If IndexOf Lambda LINQ Nothing Parse Process Property Regex Replace Select Sort Split StringBuilder SubstringA Structure is a value type. Its data, its meaning, is found directly in its bytes. Integers, Booleans and DateTimes are built-in Structures.
And:This gives Structures performance advantages—and sometimes hurts performance.
And:In Main we create an instance of Simple. We do not need to use a New Sub (a constructor).
So:A Structure, of any type, is used in the same way as an Integer. And an Integer itself is a kind of Structure.
Based on: .NET 4.5 VB.NET program that uses Structure Structure Simple Public _position As Integer Public _exists As Boolean Public _lastValue As Double End Structure Module Module1 Sub Main() Dim s As Simple s._position = 1 s._exists = False s._lastValue = 5.5 Console.WriteLine(s._position) End Sub End Module Output 1
Here:We create a DateTime structure, as a local, and initialize to a value in the year 2020.DateTime
Then:The local d2 copies the values from "d," but the two locals are separate. When "d" is changed, d2 is not affected.
VB.NET program that copies structure Module Module1 Sub Main() ' Create a structure and copy it. Dim d As DateTime = New DateTime(2020, 1, 1) Dim d2 As DateTime = d Console.WriteLine("D: " + d) Console.WriteLine("D2: " + d2) ' Reassign "d" and the copy "d2" does not change. d = DateTime.MinValue Console.WriteLine("D2: " + d2) End Sub End Module Output D: 1/1/2020 D2: 1/1/2020 D2: 1/1/2020
Here:A Structure called Box is allocated many times in a loop. The managed heap is not accessed.
And:All the Box instances are stored in local variable memory. Next a Class called Ball is allocated in a similar loop.For Loops
But:On each iteration the managed heap is accessed. This triggers garbage collection at intervals. This reduces performance.
VB.NET program that times Structure Structure Box Public _a As Integer Public _b As Boolean Public _c As DateTime End Structure Class Ball Public _a As Integer Public _b As Boolean Public _c As DateTime End Class Module Module1 Sub Main() Dim m As Integer = 100000000 Dim s1 As Stopwatch = Stopwatch.StartNew For i As Integer = 0 To m - 1 Dim b As Box b._a = 1 b._b = False b._c = DateTime.MaxValue Next s1.Stop() Dim s2 As Stopwatch = Stopwatch.StartNew For i As Integer = 0 To m - 1 Dim b As Ball = New Ball b._a = 1 b._b = False b._c = DateTime.MaxValue Next s2.Stop() Dim u As Integer = 1000000 Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) End Sub End Module Output 2.26 ns Structure 8.20 ns Class