From 33f455cd3fcd8546fc2b2de494204f51a9d9b2d9 Mon Sep 17 00:00:00 2001 From: Mostafa Kazemi Date: Sat, 24 Oct 2020 19:29:08 +0330 Subject: [PATCH] Tested IsExpression to get parent class from a child class --- IsExpressionsExample/Program.cs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/IsExpressionsExample/Program.cs b/IsExpressionsExample/Program.cs index 1788e3a..22372fa 100644 --- a/IsExpressionsExample/Program.cs +++ b/IsExpressionsExample/Program.cs @@ -10,6 +10,38 @@ class Program { static void Main(string[] args) { + + PersonCar pc = new PersonCar("Mostafa", "Kazemi", 30, "Tesla", 3); + + if (pc is Person p) + { + Console.WriteLine($"Name : {p.Name} , Family : {p.Family} , Age : {p.Age}"); + } + + Console.ReadKey(); + } + } + + class Person + { + public string Name { get; set; } + public string Family { get; set; } + public int Age { get; set; } + } + + class PersonCar : Person + { + public string CarName { get; set; } + public int CarModel { get; set; } + + + public PersonCar(string name, string family, int age, string carName, int carModel) + { + Name = name; + Family = family; + Age = age; + CarName = carName; + CarModel = carModel; } } }