Why Join
Become a member
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
C# Corner Home
Technologies
MonthlyLeaders
ASK A QUESTION
Forumguidelines
Chris Anderson
1.9k
35
676
Get property in inerrited class
Jan 13 2021 6:58 PM
I'm trying to get the property from an inherited class without creating a new instance and rather
use the parameter being passed in the method
.
How can I get the property
Color
over here?
Does not work
:
public
void
InitPlay(Toy toy)
{
Console.WriteLine($
" The {TypeOfAnimal}{Name} is now playing with the
{toy.Color}
{toy} "
);
}
Works:
This one works. But that's just because I created a new object of class Ball. But the point of passing in toy is thus redundant.
public
void
InitPlay(Toy toy)
{
Ball ball =
new
Ball();
Console.WriteLine($
" The {TypeOfAnimal}{Name} is now playing with the {ball.Color} {toy} "
);
}
Rest of the code (if needed)
class
Ball : Toy
{
public
string
Color {
get
;
set
; }
}
abstract
class
Toy
{
...
}
Reply
Answers (
5
)
How to nest lists in list?
G-suite Integration with a web application Google workspace