C
C#2y ago
Kosta

❔ Why do we need to provide values for base class we are inheriting from?

For example i have this:
public class Answer:Entity<Guid>
{

protected Answer()
{

}
public Answer(Guid id) : base(id)
{

}
}
public class Answer:Entity<Guid>
{

protected Answer()
{

}
public Answer(Guid id) : base(id)
{

}
}
Why do i need to base the Id back to the base class? what does it even do? How does the flow work? How does it know to set the id of the answer class because we are passing it to the base class?
3 Replies
Thinker
Thinker2y ago
base(id) just calls the constructor of the base class with the argument id The constructor of the base class likely sets some Id property in the class itself to the id passsed into the constructor
Kosta
KostaOP2y ago
I mean this is the class:
public abstract class Entity<TKey> : Entity, IEntity<TKey>
{
/// <inheritdoc/>
public virtual TKey Id { get; protected set; }

protected Entity()
{

}

protected Entity(TKey id)
{
Id = id;
}

public override object[] GetKeys()
{
return new object[] { Id };
}

/// <inheritdoc/>
public override string ToString()
{
return $"[ENTITY: {GetType().Name}] Id = {Id}";
}
}
public abstract class Entity<TKey> : Entity, IEntity<TKey>
{
/// <inheritdoc/>
public virtual TKey Id { get; protected set; }

protected Entity()
{

}

protected Entity(TKey id)
{
Id = id;
}

public override object[] GetKeys()
{
return new object[] { Id };
}

/// <inheritdoc/>
public override string ToString()
{
return $"[ENTITY: {GetType().Name}] Id = {Id}";
}
}
I just dont get how from this methode:
protected Entity(TKey id)
{
Id = id;
}
protected Entity(TKey id)
{
Id = id;
}
We get the Id in our new initialized class
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server