Call Abstract Constructor in Another Time - C#

Shervan360 1,661 Reputation points
2023-01-14T03:27:34.3433333+00:00

Hello,

I have two classes. Can I call the abstract constructor after a modified argument in an extended class? For example:

namespace OOPAdventure;

public abstract class Character
{
    public String Name { get; }
    public Character(string name) => Name = name;
}


namespace OOPAdventure;

public class Player : Character
{
    public Player(string name) : base(name)
    {
        name = name.Remove(2);
        // NOW, Can I send modified 'name' to the Charecter class?
    }
}
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-01-14T06:55:24.08+00:00

    In your particular case, try this:

    public class Player : Character
    {
        public Player( string name ) : base( name.Remove( 2 ) )
        {
        }
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.