C++ struct initializing in C#
in C++, we can do this -
But I found that I can't do this in C#. Do I have to assign values by each one like this?
I want to know how to assign member values simply in C#.
7 Replies
Foo a = new Foo{ A = 5, B = 7 };
however you can do Foo a = (5, 7);
if you defined an implicit operator that has a parameter of a specific ValueTupleSo I have to make an operator if I don't want
new
?TheRanger
REPL Result: Success
Console Output
Compile: 530.825ms | Execution: 42.423ms | React with ❌ to remove this embed.
yes
C# probably didnt allow this syntax
Foo a = { 5, 7 };
due to safety reasonsI see
Thanks!
is also valid
i think
new
struct means that it gets allocated on the heap in c++? in c# thats not always the case. it allocates on the stack in your method but ofc if you assign it to a field in a class which is on the heap then its on the heap but thats the same as in c++