C
C#2mo ago
cheeze2000

Trying to understand required members

I can't seem to set the value of a required member through a subclass's constructor. https://dotnetfiddle.net/Gz6b7M Any help is appreciated.
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
8 Replies
ero
ero2mo ago
required is purely for the object initializer What you're seeing is intended
cheeze2000
cheeze2000OP2mo ago
got it, thanks how do i handle this "non-nullable property must contain a non-null value when exiting constructor" warning?
ero
ero2mo ago
I agree it would be nice if the code knew the value is technically not "required" in the child class, but that's unfortunately not how c# works
cheeze2000
cheeze2000OP2mo ago
yea the keyword is a little misleading this warning appears when i remove the required keyword
ero
ero2mo ago
I would give Animal a constructor that takes a string And inheriting classes can call the : base() constructor
cheeze2000
cheeze2000OP2mo ago
got it thanks thanks i'll take a look at it
ero
ero2mo ago
What the That's new to me
Anton
Anton2mo ago
Make a factory function for dog
Want results from more Discord servers?
Add your server