I have this code:
public abstract class Animal
{
public string Name { get; set; }
}
public class Dog : Animal
{
[JsonProperty("name")]
public new string Name { get; set; }
}
public static void Main()
{
var dog = new Dog
{
Name = "Spark"
};
Console.WriteLine(dog.Name);
Console.WriteLine(((Animal)dog).Name == null);
}
Which will output:
Spark
True
Why is the Name
property null
when casting it to Animal
?
How to fix it, if I want to cast my object to Animal
? How can I keep the properties values?