DNK has a spanking new server up and running like clockwork. Stability problems are a thing of the past. Our new monitoring service reports 100% uptime since the move!
46
kicks
published 7 months, 24 days ago, submitted by samdnp 8 months, 1 day ago

dotnetperls.com — 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!

Add a comment 11 comments | category: | Views: 938 | Get KickIt image code
C# Structs
tags: | tag it

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:
Please leave comments/corrections/criticisms on the article here, thanks! Sam
posted by samdnp 7 months, 24 days ago
I'd love to, but all I get is "The XML page cannot be displayed" when I go to the article.
posted by tortus 7 months, 24 days ago
ah excellent, thanks!
posted by tortus 7 months, 24 days ago
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
posted by DamienG DamienG 7 months, 23 days ago
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.
posted by samdnp 7 months, 23 days ago
I really liked this article. Thanks!
posted by AndrewVos 7 months, 23 days ago
I'm glad you liked it Andrew! Thanks
posted by samdnp 7 months, 23 days ago
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.
;)
posted by regev regev 7 months, 23 days ago
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.
posted by samdnp 7 months, 23 days ago
[comment removed]
posted by ipich 7 months, 22 days ago



information Login or create an account to comment on this story

Related Stories:
 

Search: