C
C#3w 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
ero3w ago
required is purely for the object initializer What you're seeing is intended
cheeze2000
cheeze2000OP3w ago
got it, thanks how do i handle this "non-nullable property must contain a non-null value when exiting constructor" warning?
ero
ero3w 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
cheeze2000OP3w ago
yea the keyword is a little misleading this warning appears when i remove the required keyword
ero
ero3w ago
I would give Animal a constructor that takes a string And inheriting classes can call the : base() constructor
cheeze2000
cheeze2000OP3w ago
got it thanks thanks i'll take a look at it
ero
ero3w ago
What the That's new to me
Anton
Anton3w ago
Make a factory function for dog
Want results from more Discord servers?
Add your server