Which method should I prefer and why? Is there any real difference?
Abstract property:
abstract class Table
{
public abstract string Title { get; }
}
class InfoTable : Table
{
public override string Title
{
get { return "Info"; }
}
}
or base class constructor parameter:
abstract class Table
{
public string Title { get; private set; }
public Table(string title)
{
Title = title;
}
}
class InfoTable : Table
{
public InfoTable() : base("Info") { }
}
As they stand your class are just data class which is not a good OOP practice. Anyway I prefer the first type because if in that case in a subclass you can have a non harcoded title as in this example
class InfoTable : Table
{
private string id;
private string name;
public override string Title
{
get { return name+id; }
}
}
but still it may depend which of the two is better
Okay let's say that the class is not something as static as this example then
private DynamicTitleProvider provider;
public override string Title
{
get { return provider.GetTitle(); }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With