C# Structs(dotnetperls.com)

submitted by samdnpsamdnp(975) 3 years, 3 months ago

What are the benefit of structs in C#? When can they improve performance and memory use? See examples and benchmarks as well as screens from CLRProfiler and the Visual Studio debugger. Is this is the best struct article in the world? Maybe!

11 comments |category: |Views: 951

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by samdnpsamdnp(975) 3 years, 3 months ago 0

Please leave comments/corrections/criticisms on the article here, thanks! Sam

Reply

posted by tortustortus(25) 3 years, 3 months ago 0

I'd love to, but all I get is "The XML page cannot be displayed" when I go to the article.

Reply

posted by tortustortus(25) 3 years, 3 months ago 0

ah excellent, thanks!

Reply

posted by DamienGDamienG(1405) 3 years, 3 months ago 0

Be aware of the overheads and semantics of passing structs into methods, e.g.

public void SetDetails(Employee a)
{
a.Name = "Bob";
a.Age = 30;
}

Will work fine where Employee is a class but will appear to do nothing if it is a struct as they are passed by value not reference so the original object will not be changed. It also means the entire struct will be pushed onto the stack value by value not just the memory pointer which may have performance overheads especially in recursive code.

If you are going to use structs you may want to consider making the objects immutable, i.e. they never change value once instantiated but instead create new versions. This is what many of the value types (structs) in .NET already do, e.g. DateTime.Now.AddDays(1) doesn't modify Now, it creates a new instance from it.

[)amien

Reply

posted by samdnpsamdnp(975) 3 years, 3 months ago 0

Good comment Damien. It would be good for me to add a part about the out and ref keywords with structs.

It would be neat to test at what point passing by value becomes less efficient than by reference.

Reply

posted by AndrewVosAndrewVos(0) 3 years, 3 months ago 0

I really liked this article. Thanks!

Reply

posted by samdnpsamdnp(975) 3 years, 3 months ago 0

I'm glad you liked it Andrew! Thanks

Reply

posted by regevregev(20) 3 years, 3 months ago 0

Another implication of the stack versus heap issue might be surprising to some. Consider the following:
I declare a struct:

struct A
{
public bool B;
}

Now guess what will happen if try to use it as follows:

A a = new A(); // Create an instance.
List<A> list = new List<A>();
list.Add(a); // Add to a list.
list[0].B = false; // Change the bool value.

What happens is compilation error on the last line! The error will not happen if I use class instead of struct:

class A
{
public bool B;
}


Have a nice day.
;)

Reply

posted by samdnpsamdnp(975) 3 years, 3 months ago 0

Regev, that's a good point. I pasted your code into VS and it complains about "not a variable". Coming from C it is a bit confusing to me.

I hope to add some material from the comments soon.

Reply

information Login or create an account to comment on this story