Why is the struct allowed but the class isn't?

The error is CS8618 - Non-nullable variable must contain a non-null value when exiting constructor. Consider declaring it as nullable. The struct is readonly if that matters.
No description
27 Replies
phaseshift
phaseshift2d ago
you need to init the struct It's not clear to me which struct you are referencing when saying a struct is allowed
ElectricTortoise
ElectricTortoiseOP2d ago
oh struct is PositionInfo light green = struct green = class sorry i shouldve been clearer
phaseshift
phaseshift2d ago
what are the members of BitBoard? And does it have a non-default ctor?
ElectricTortoise
ElectricTortoiseOP2d ago
members?
phaseshift
phaseshiftthis hour
since it's readonly you must init it with the correct data to start with
ElectricTortoise
ElectricTortoiseOPthis hour
PositionInfo is readonly, Bitboard is not
ElectricTortoise
ElectricTortoiseOPthis hour
this is what Bitboard looks like
No description
phaseshift
phaseshiftthis hour
So class members dont get default init'd to any specific value. They stay as null unless to write code otherwise
ElectricTortoise
ElectricTortoiseOPthis hour
ooo ok but structs do?
phaseshift
phaseshiftthis hour
(a member that is a class type)
ElectricTortoise
ElectricTortoiseOPthis hour
ok or is it bcs the struct is readonly
phaseshift
phaseshiftthis hour
yeah, structs are always non-null
ElectricTortoise
ElectricTortoiseOPthis hour
ah ok so Bitboard? will fix it?
phaseshift
phaseshiftthis hour
you can either let null be a valid state for it (not suggested) by changing the type to BitBoard?, or you can init it inline, BitBoard _bitboard = new();, or you can do similar in a constructor If BitBoard has constructor arguments then add them into new(...)
ElectricTortoise
ElectricTortoiseOPthis hour
ooo ok whats inline btw
phaseshift
phaseshiftthis hour
BitBoard _bitboard = new(); it's what I'm calling that where the init is 'inline' with the member declaration
ElectricTortoise
ElectricTortoiseOPthis hour
ooo ok what does the _ do in python it makes the field protected does it do anything in C#
glhays
glhaysthis hour
Does nothing, just a naming convention adopted by many.
ElectricTortoise
ElectricTortoiseOPthis hour
ooo ok
phaseshift
phaseshiftthis hour
it doesnt do anything, just convention
ElectricTortoise
ElectricTortoiseOPthis hour
wait but wouldnt my variable name be _bitboard instead of bitboard
glhays
glhaysthis hour
Yes
ElectricTortoise
ElectricTortoiseOPthis hour
do i have to fix it later on? or do i just use _bitboard
phaseshift
phaseshiftthis hour
It would change the name, yes it needs to match everywhere. You dont need to change it
ElectricTortoise
ElectricTortoiseOPthis hour
ok ty for the help
phaseshift
phaseshiftthis hour
👍
Anton
Anton7h ago
The compiler does not check for null references in struct fields. You would be getting an error there as well, because you have an uninitialized reference type field in PositionInfo.
Want results from more Discord servers?
Add your server