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.
27 Replies
you need to init the struct
It's not clear to me which struct you are referencing when saying a struct is allowed
oh
struct is PositionInfo
light green = struct
green = class
sorry i shouldve been clearer
what are the members of BitBoard?
And does it have a non-default ctor?
members?
since it's readonly you must init it with the correct data to start with
PositionInfo is readonly, Bitboard is not
this is what Bitboard looks like
So class members dont get default init'd to any specific value. They stay as null unless to write code otherwise
ooo ok
but structs do?
(a member that is a class type)
ok
or is it bcs the struct is readonly
yeah, structs are always non-null
ah ok
so Bitboard? will fix it?
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(...)
ooo ok
whats inline btw
BitBoard _bitboard = new();
it's what I'm calling that where the init is 'inline' with the member declarationooo ok
what does the _ do
in python it makes the field protected
does it do anything in C#
Does nothing, just a naming convention adopted by many.
ooo ok
it doesnt do anything, just convention
wait but wouldnt my variable name be _bitboard instead of bitboard
Yes
do i have to fix it later on? or do i just use _bitboard
It would change the name, yes it needs to match everywhere. You dont need to change it
ok
ty for the help
👍
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.