C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: Second, you can see the class declared, which contains two reference value members.
Tip: When used, the two types will be stored in different memory locations (stack and heap).
Example type declarations: C#
struct ExampleStruct
{
public int[] _value1;
public int[] _value2;
public ExampleStruct(int[] value1, int[] value2)
{
this._value1 = value1;
this._value2 = value2;
}
}
class ExampleClass
{
public int[] _value1;
public int[] _value2;
public ExampleClass(int[] value1, int[] value2)
{
this._value1 = value1;
this._value2 = value2;
}
}
MethodStruct: When you call this method, the struct and all its fields are copied onto the function stack. It takes time to copy larger structs.
MethodClass: This receives a strongly-typed value containing a reference to an ExampleClass object. The parameter here is a reference.
Tip: Reference values are basically pointers and are very lightweight and efficient. Instantiating objects is more expensive.
C# program that uses struct and class parameters
using System;
class Program
{
static int MethodStruct(ExampleStruct example)
{
return example._value1[0];
}
static int MethodClass(ExampleClass example)
{
return example._value1[0];
}
static void Main()
{
//
// Create a new array with value 5.
//
int[] array1 = new int[1];
array1[0] = 5;
Console.WriteLine("--- Array element assigned ---");
//
// Create new struct and class.
//
ExampleStruct example1 = new ExampleStruct(array1, array1);
ExampleClass example2 = new ExampleClass(array1, array1);
//
// Use struct and then class in method as parameters.
//
int value1 = MethodStruct(example1);
Console.WriteLine(value1);
int value2 = MethodClass(example2);
Console.WriteLine(value2);
//
// Change the value of the array element.
//
array1[0] = 10;
Console.WriteLine("--- Array element changed ---");
int value3 = MethodStruct(example1);
Console.WriteLine(value3);
int value4 = MethodClass(example2);
Console.WriteLine(value4);
}
}
Output
--- Array element assigned ---
5
5
--- Array element changed ---
10
10
Data types tested: C#
struct A
{
public int[] a;
public int[] b;
}
class B
{
public int[] a;
public int[] b;
}
Dictionary used to store objects: C#
var d1 = new Dictionary<string, A>();
var d2 = new Dictionary<string, B>();
d1.Add("a", new A());
d2.Add("a", new B());
Code tested in loop: C#
A a1;
if (d1.TryGetValue("a", out a1))
{
var x = a1.a;
}
B b1;
if (d2.TryGetValue("a", out b1))
{
var x = b1.a;
}
Output
100000000 iterations tested.
Also tested in reverse order (class then struct).
A) Struct used: 3721 ms
B) Class used: 3595 ms [faster]
And: This means that when the struct increases the number of fields, each method call such as TryGetValue becomes more expensive.
3 fields
Struct: 3907 ms
Class: 3733 ms
4 fields
Struct: 4803 ms
Class: 3634 ms
Tip: I recommend only using structs when the design of your application is complete.
And: Before adding custom structs, be sure to perform benchmarking that proves structs are beneficial.