Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a quick way of zeroing a struct in C#?

Tags:

c#

struct

This must have been answered already, but I can't find an answer:

Is there a quick and provided way of zeroing a struct in C#, or do I have to provide someMagicalMethod myself?

Just to be clear, I know the struct will be initialised to 0, I want to know if there's a quick way of resetting the values to 0.

I.e.,

struct ChocolateBar {
    int length;
    int girth;
}

static void Main(string[] args) {
    ChocolateBar myLunch = new ChocolateBar();
    myLunch.length = 100;
    myLunch.girth  = 10;

    // Eating frenzy...
    // ChocolateBar.someMagicalMethod(myLunch);

    // myLunch.length = 0U;
    // myLunch.girth = 0U;
}
like image 556
Ed King Avatar asked Dec 10 '13 13:12

Ed King


People also ask

How do you clear a struct value?

The easiest solution is to write a method which does a reset of all the individual members. In C, we use memset(&struct_var, 0, sizeof(struct whatever)) when we know for sure that 0 or NULL is an acceptable initial value for all of its members. But in C++, it is tough to keep this assumption true always.

Does C initialize struct to 0?

You don't have to initialise every element of a structure, but can initialise only the first one; you don't need nested {} even to initialise aggregate members of a structure. Anything in C can be initialised with = 0 ; this initialises numeric elements to zero and pointers null.

How do you initialize a struct in C?

Structure members can be initialized using curly braces '{}'.

Can we use memset for structure in C?

memset will set the structure to all-bits-zero whereas value initialization will initialize all members to the value zero. The C standard guarantees these to be the same only for integral types, not for floating-point values or pointers. Also, some APIs require that the structure really be set to all-bits-zero.


2 Answers

Just use:

myLunch = new ChocolateBar();

or

myLunch = default(ChocolateBar);

or

myLunch = default;

These are equivalent1, and will both end up assigning a new "all fields set to zero" value to myLunch.

Also, ideally don't use mutable structs to start with - I typically prefer to create a struct which is immutable, but which has methods which return a new value with a particular field set differently, e.g.

ChocolateBar myLunch = new ChocolateBar().WithLength(100).WithGirth(10);

... and of course provide appropriate constructors as well:

ChocolateBar myLunch = new ChocolarBar(100, 10);

1 At least for structs declared in C#. Value types can have custom parameterless constructors in IL, but it's relatively hard to predict the circumstances in which the C# compiler will call that rather than just use the default "zero" value.

like image 166
Jon Skeet Avatar answered Oct 21 '22 16:10

Jon Skeet


Just call the parameterless constructor in your code:

ChocolateBar chocolateBar = new ChocolateBar();
like image 35
Ufuk Hacıoğulları Avatar answered Oct 21 '22 17:10

Ufuk Hacıoğulları