Cant initialize a dictionary list in c# in abstract class where dictionary is static

55 Views Asked by At

i have this abstract class where i try to initialize dictionary with Objects but someone it wont let me

public abstract class ICharacterBody3DState
{
    
    public static Dictionary<StateInput, ICharacterBody3DState> State = new Dictionary<StateInput, ICharacterBody3DState>()
    {
        StateInput.sprint=new StateInput(),

    };

}

Each state is inheriting from this abstract class :

public class WalkingState : ICharacterBody3DState
{
}

but it gives me : CS0747: Invalid initializer member declarator. is there any way to initilize the abstract class Dictionary with Objects ?

1

There are 1 best solutions below

1
Suryateja KONDLA On BEST ANSWER
public abstract class ICharacterBody3DState
{
    public static Dictionary<StateInput, ICharacterBody3DState> State = new Dictionary<StateInput, ICharacterBody3DState>()
    {
        { StateInput.sprint, new StateInput() },
    };
}

put each key and value inside curly braces {} and separated them with a comma. This is the right way to add items to your dictionary.