Creating an Overridable Enumeration in a Parent Class

I want to create a nested structure where each class represents a country inheriting the same parent class Country. Each child class must have an enumeration representing different states States.

The goal is to select a country, and then one of its states.

The content will be saved in the dictionary Dictionary<Tuple<string, Type>, object>, where and Typewill be .CountryCountry.States

I tried to create a interface/ abstract classc enumunder the name Statesthat will be implemented, but this does not work as it is a type definition.

Is there any workaround?

public abstract class Country
{
    public abstract enum States { get; }
}

public class CountryA : Country
{
    public new enum States
    {
        StateA,
        StateB,
        StateC,
    }
}
+4
source share
5 answers

, Country , . public string[] States { get; set; }.

() Country, States, :

var usa    = new Country { Name = "USA",    States = new[] { "Alabama", ... } };
var canada = new Country { Name = "Canada", States = new[] { ... } };
// etc
+2

:

(. : ), , , , - .

enum enum (. : typeafe enum pattern), , , .

- "" . , // .. . , , , .

!

[ camilo-terevinto]

+1

, , , , , , , , " ".

, generics ( , Enum s):

public abstract class Country<TStates> 
    where TStates: struct, IConvertible, IFormattable, IComparable
{
    public abstract TStates[] States { get; }
}

public enum UnitedStatesStates
{
    WhoCares, WhoCares2
}

public class UnitedStatesCountry : Country<UnitedStatesStates>
{
    public override UnitedStatesStates[] States { get; }
}

, ( ) .

+1

, - , , , , (!) .

, , - .

,

public abstract string[] States {get;}

, . , , , - .

0

enum , , enum, static public ( ). :

public class Country1
{
    public static State State1 { get; } = new State("State 1");
    public static State State2 { get; } = new State("State 2");
    ...
}

, Country1.State1, ? State , string. , , .

You can follow the same principle for implementing a long chain of objects: Planet.Continent.Country.State.Province.Town.Street.Hause..


You speak

Content will be saved in the dictionary Dictionary<Tuple<string, Type>, object>, where Types will be Countryand Country.States.

not to do. These are different types, which is a poor choice of key. If you need to list (find) states, just add another element to Country:

public static IEnumerable<State> States
{
    get
    {
        yield return State1;
        yield return State2;
        ...
    }
}

Then finding something might be simple linq:

var stateAInCountry1 = ...Countries.OfType<Contry1>().Single().States.Single(o => o.Name == "A");
var countriesWithStateA = ...Countries.Where(o => o.States.Any(o => o.Name == "A"));

You do not know what problem you are solving by entering a dictionary, but you can initialize an additional data structure with the correct key, if you have provided the possibility of iteration with ease.

0
source

Source: https://habr.com/ru/post/1695692/


All Articles