so... like Rust?
so... like Rust?
Without unsafe, zero init is not needed.
This is opposite to the way unsafe (either syntax or known unsafe APIs) is used today.
All use of p/invoke is also unsafe though, even if the keyword isn’t used. And it’s much more common to wrap a C library than to write a buffer pool.
In C#, to have all zeroes, you would do "Guid.Empty"
"new Guid()" is equiverlant "Guid.Empty"
Personally, I would prefer writing "Guid.Empty" as it is cleaner. It also helps ensure you understand reading other developers code... their intensions.
Afterall, a lesser experienced developer may not know (or simply forgot) that "new Guid()" does not create a new, unique value. So writing "new Guid()" looks more misleading.
var myGuid1 = new Guid(); // all zeroes, or a value?
var myGuid2 = Guid.Empty; // Ah.. all zeroes.
It is ESPECIALLY cleaner when doing comparison :-
if(myGuid2 == Guid.Empty) { }
To set a Guid, you would do :-
myGuid2 = Guid.NewGuid(); // This makes sense.
If supplying Guid is optional, you just make it Guid?.
To be fair, I don't think offering default(T) by default (ha) is the best choice for structs. In F#, you have to explicitly do `Unchecked.defaultOf` and otherwise it will just not let you have your way - it is watertight. I much prefer this approach even if it can be less convenient at times.
Java has the same problem.
(Dart, which I work on, does not. In Dart, you really truly can't observe an instance field before it has been initialized.)