in Technology by
When should I use a struct rather than a class in C#?

1 Answer

0 votes
by

Whenever you:

  1. don't need polymorphism,
  2. want value semantics, and
  3. want to avoid heap allocation and the associated garbage collection overhead.

The caveat, however, is that structs (arbitrarily large) are more expensive to pass around than class references (usually one machine word), so classes could end up being faster in practice

Related questions

0 votes
    Why Should I Use Numpy Rather Than Idl, Matlab, Octave, Or Yorick?...
asked Apr 25, 2021 in Technology by JackTerrance
0 votes
    _____ prevents you from seeing an individual as an individual rather than as a member of a group. (a) Cultural ... (c) Schematas (d) Attributions Please answer the above question....
asked Oct 22, 2022 in Education by JackTerrance
0 votes
    The question as the title. Why is this, I have used the ssh command: ssh -i mykey.pem root@xxx-xxx-xx-xx-xxx ... What am I doing wrong? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
0 votes
    if both A and R are true and R is the correct explanation of A if both A and R are true ... questions and answers pdf, Verbal Reasoning interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    I have an issue with passing a struct definition to a function. Not an instance of a struct, but the definition. We ... (object sender, EventArgs e) { GetHeaders( MineStruct ); //...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I'm trying to PInvoke into this C++ library function: int Initialize(Callback* callback); struct Callback { ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I'm trying to PInvoke into this C++ library function: int Initialize(Callback* callback); struct Callback { ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I'm coding a basic portfolio page in ASP.NET Core Razor Pages. I want to have a contact form at ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Closed 10 years ago. Possible Duplicate: What are the differences ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Closed 10 years ago. Possible Duplicate: What are the differences ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    I am studying Artificial Intelligence as a module in my Computer Games Programming course and one of my tasks ... appreciated, thanks. Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    What should we use to access elements with a value greater than five? (a) Subsetting commands (b) Use ... Out of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    I want to implement in Java a class for handling graph data structures. I have a Node class and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 27, 2022 in Education by JackTerrance
...