You can't - and your example shows why: your two derived classes do not have the same properties, and do not share a "family" property.
So what would happen in the base class if you created an instance of a derived class which doesn't contain the property? it can't access it, so you application falls over.
You can do it, but only if you configure your abstract base class to have an abstract property which all derived classes must implement:
abstract class Person
{
public abstract string Family { get; set; }
}
You can then access the Family property in all derived classes from your based class code.
"Is it possible define a base class then inherit it and check derived class's property value through base class method? please guide me"
If the property is declared as abstract in the base class so that all derived classes have to implement it in order to compile, then the base class can access the derived class value in base class code:
abstract class Person
{
public abstract string Family { get; set; }
public void ShowMe()
{
Console.WriteLine(Family);
}
}
That's fine, because the abstract keyword ensures that the Family property must be implemented in all derived classes. However, the base class can't get at anything the derived class uses to generate the values in the property:
abstract class Person
{
public abstract string Family { get; set; }
public void ShowMe()
{
Console.WriteLine(Family);
}
}
class Individual : Person
{
public string HyphenatedStart { get; set; }
public string HyphenatedEnd { get; set; }
public override string Family
{
get
{
return string.Format("{0}-{1}", HyphenatedStart, HyphenatedEnd);
}
set
{
string[] parts = value.Split('-');
HyphenatedStart = parts[0];
HyphenatedEnd = parts[1];
}
}
}
The Person.ShowMe method can't access HyphenatedStart or HyphenatedEnd because they don't exist as part of the Person class, and so aren't guaranteed to exist in all derived classes - even though they are
public
values in the Individual class.