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.
Ie,
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;
}
Just use:
myLunch = new ChocolateBar();
or
myLunch = default(ChocolateBar);
or
myLunch = default;
These are equivalent 1 , 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, eg
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.
只需在代码中调用无参数构造函数:
ChocolateBar chocolateBar = new ChocolateBar();
A new ChocolateBar
is initialized to zero. so:
myLunch = new ChocolateBar();
This only works because ChocolateBar
is 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.
struct
s 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.
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.
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.