C
C#5mo ago
Surihia

A small help in understanding structs:

I am learning how to use structs and have a question. for this question I decided to write a small test program by using some structs.
Inside Program.cs
c#
using System;
using static TestStuff.Class2;

namespace TestStuff
{
internal class Program
{
static void Main(string[] args)
{
var testHoldStruct = new TestHold();
var testHoldStruct2 = new TestHold2();

testHoldStruct.val1 = 100;
testHoldStruct.val2 = 500;

testHoldStruct2.val2_1 = 1000;
testHoldStruct2.val2_2 = 5000;

Class1.ProcessTest(testHoldStruct, testHoldStruct2);

Console.WriteLine("");
Console.WriteLine("Done");
Console.ReadLine();
}
}
}
c#
using System;
using static TestStuff.Class2;

namespace TestStuff
{
internal class Program
{
static void Main(string[] args)
{
var testHoldStruct = new TestHold();
var testHoldStruct2 = new TestHold2();

testHoldStruct.val1 = 100;
testHoldStruct.val2 = 500;

testHoldStruct2.val2_1 = 1000;
testHoldStruct2.val2_2 = 5000;

Class1.ProcessTest(testHoldStruct, testHoldStruct2);

Console.WriteLine("");
Console.WriteLine("Done");
Console.ReadLine();
}
}
}
Inside Class1.cs file:
c#
using System;
using static TestStuff.Class2;

namespace TestStuff
{
internal class Class1
{
public static void ProcessTest(TestHold testHoldStruct, TestHold2 testHold2Struct)
{
Console.WriteLine(testHoldStruct.val1);
Console.WriteLine(testHoldStruct.val2);

Console.WriteLine(testHold2Struct.val2_1);
Console.WriteLine(testHold2Struct.val2_2);
}
}
}
c#
using System;
using static TestStuff.Class2;

namespace TestStuff
{
internal class Class1
{
public static void ProcessTest(TestHold testHoldStruct, TestHold2 testHold2Struct)
{
Console.WriteLine(testHoldStruct.val1);
Console.WriteLine(testHoldStruct.val2);

Console.WriteLine(testHold2Struct.val2_1);
Console.WriteLine(testHold2Struct.val2_2);
}
}
}
Inside Class2.cs file:
c#
namespace TestStuff
{
internal class Class2
{
public struct TestHold
{
public int val1;
public int val2;
}

public struct TestHold2
{
public int val2_1;
public int val2_2;
}
}
}
c#
namespace TestStuff
{
internal class Class2
{
public struct TestHold
{
public int val1;
public int val2;
}

public struct TestHold2
{
public int val2_1;
public int val2_2;
}
}
}
Am I correct in assuming that when I am calling ProcessTest method from Program.cs file, a copy of the struct object is created in memory when Class1 is being processed ? I assume that if I use a class instead of a struct for ProcessTest method, I would only be passing it as a reference to the class object and in that case, a copy of the object isn't created when Class1 is being processed
7 Replies
Kouhai /人◕ ‿‿ ◕人\
Yes the struct is copied
MODiX
MODiX5mo ago
Kouhai /人◕ ‿‿ ◕人\
REPL Result: Success
var s = new TestStruct() {val = 10};
var c = new TestClass() {val = 10};
Inc(c, s);
Console.WriteLine(s.val);
Console.WriteLine(c.val);
void Inc(TestClass c, TestStruct s)
{
c.val++;
s.val++;
}
struct TestStruct { public int val; }
class TestClass { public int val; }
var s = new TestStruct() {val = 10};
var c = new TestClass() {val = 10};
Inc(c, s);
Console.WriteLine(s.val);
Console.WriteLine(c.val);
void Inc(TestClass c, TestStruct s)
{
c.val++;
s.val++;
}
struct TestStruct { public int val; }
class TestClass { public int val; }
Console Output
10
11
10
11
Compile: 467.630ms | Execution: 40.203ms | React with ❌ to remove this embed.
Kouhai /人◕ ‿‿ ◕人\
You can see here that val for TestClass was modified because it was passed by by reference
Surihia
Surihia5mo ago
Is there a performance hit by doing it like this ? would classes be a better option for this type of program ?
Kouhai /人◕ ‿‿ ◕人\
Depends on what you're going to do, if you're gonna box it a lot or you want the type to be mutable then go with a class, if not then a struct would be good. Also it's recommended to keep the structs below 16 bytes.
Surihia
Surihia5mo ago
what do you mean by box it ?
Kouhai /人◕ ‿‿ ◕人\
Boxing and Unboxing - C# Programming Guide - C#
Learn about boxing and unboxing in C# programming. See code examples and view additional available resources.
Want results from more Discord servers?
Add your server
More Posts