Ed King
Ed King

Reputation: 1863

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

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;
}

Upvotes: 18

Views: 8539

Answers (6)

8Unlimited8
8Unlimited8

Reputation: 494

You can modify your struct in this way:

struct ChocolateBar {
    int length;
    int girth;
    public void Clear()
    {
        this = new ChocolateBar();
    }
}

and use it in your code easily:

ChocolateBar cb = new ChocolateBar();
//some stuff here
cb.Clear();

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501163

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.

Upvotes: 28

Just add Zero into your struct, as shown below. Also, as a side point, consider using constructors in your structs, so that you can parameterize your variables instead of setting them individually:

public struct ChocolateBar {
    int length;
    int girth;

    public static ChocolateBar Zero { get; }

    public ChocolateBar(int length, int girth) {
        this.length = length;
        this.girth = girth;
    }
}

class OtherClass() {
    ChocolateBar cB = new ChocolateBar(5, 7);
    cB = ChocolateBar.Zero();

    Console.Writeline( (cB.length).ToString() ); // Should display 0, not 5
    Console.Writeline( (cB.girth).ToString() ); // Should display 0, not 7
}

The reason why Zero gets the 0 values, is because the default (static) values of int length and int girth are 0, as mentioned by others above. And Zero is itself static, so you can access it directly without an object reference.

In other words, most (if not all) your structs should have a Zero property, as well as a constructor. It's super useful.

Upvotes: 0

Peter
Peter

Reputation: 5728

A new ChocolateBar is initialized to zero. so:

myLunch = new ChocolateBar();

This only works because ChocolateBaris a struct/value type. If ChocolateBar were a class, this would create a new ChocolateBar and change myLunch to point to it. The values of the ChocolateBar stored in myLunch would be zero. The old ChocolateBar would be unchanged, and eventually be claimed by the garbage collector, unless some other reference pointed to the old myLunch too.

Upvotes: 6

Shaharyar
Shaharyar

Reputation: 12449

structs are a value type. They are set to zero by default when you initialize it.

int default value is zero. You don't have any need to set it to zero.

Upvotes: 2

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38478

Just call the parameterless constructor in your code:

ChocolateBar chocolateBar = new ChocolateBar();

Upvotes: 13

Related Questions