简体   繁体   中英

Are hard coded arrays on classes optimized by the compiler(s)?

I am using the newest version of Visual Studio Community 2017 in the default configuration with all the newest updates.

I am writing a system which processes items with different properties. They may contain any combination of properties, each property requiring one or more data members and unique functionality to process them. To allow me (or any developer) to implement new functionality quickly and to reduce the amount of code I'll have to write, I'm using inheritance.

The problem is that each subclass which provides the data and functions needs certain data which is essentially static, however c# does not allow overriding of static members. My solution is to override the base class members and just hard code the data in their getter methods, like this:

public class BaseClass
{
    public abstract string name { get; }
    public abstract Color color { get; }
    public abstract string[] info { get; }
    public abstract OtherClass[] otherClasses { get; }
}

public class SubClass : BaseClass
{
    public override string name { get { return "flip flops"; } }
    public override Color color { get { return Color.FromArgb(255, 0, 255, 255); } }
    public override string[] info { get { return new string[] { "a", "b", "c" }; } }
    public override OtherClass[] otherClasses {
        get {
            return new otherClass[] {
                new OtherClass("hard"),
                new OtherClass("coded"),
                new OtherClass("data") }
        }
    };
}

As you can see some of the members are value types, some are classes and some are arrays of classes. My concern is that if I were to have 1000 instances of SubClass, the runtime environment would create unique instances of each member for each class. This is completely unnecessary since the data for each would be identical. Color is a struct and therefore a value type, so I'm not sure if it would react any differently.

I tried to (and did) solve the problem already using another class. SubClassData stores the members, and each SubClass references a SubClassData assigned through the class constructor. The SubClassData classes are stored in a large static array. Firstly, using this it is possible to assign the wrong SubClassData to the SubClass on construction. More importantly this pattern separates the data from from the functionality which feels completely unnatural. I think those are fundamentally the same problem.

Currently I'm working on using the first method shown to implement the feature. I'm not sure if this is micro-optimization, whether c# takes care of this or if there's a better design pattern to use to achieve this, but all feedback is appreciated :)

You could simply return data from the public getters that you've defined statically and privately:

public class SubClass : BaseClass
{
    private const string _name = "flip flops";
    private static readonly Color _color = Color.FromArgb(255, 0, 255, 255);
    private static readonly string[] _info = new string[] { "a", "b", "c" };
    private static readonly OtherClass[] _otherClasses = new otherClass[] {
        new OtherClass("hard"),
        new OtherClass("coded"),
        new OtherClass("data") 
    };

    public override string name { get { return _name; } }
    public override Color color { get { return _color; } }
    public override string[] info { get { return _info; } }
    public override OtherClass[] otherClasses { get { return _otherClasses; } }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM