简体   繁体   中英

I think I need a static class with a constructor?

So, apologies in advance for the surely poor terminology in this question - I'm trying to teach myself C#, and struggling with a few concepts.

In pseudo-code, I have something like this:

static void Main()
{
    // This will create lots of "Item"(s), and do stuff with them
}
public class Item
{
    // a bunch of properties
    public Item()
    {
    }
}

Next, I need a UtilityFunction1 that'll do some work - it will be called many times during Main() and passed Item s to do some calculations with them. This function needs to set up a Dictionary to do its job. Even though it will be called many times, the Dictionary should only be set up once.

I also need a UtilityFunction2 . Like UtilityFunction1 , this function also needs a Dictionary to do it's job, which should only be set up once. This function will be called many times by UtilityFunction1 .

So, on one hand, it seems like UtilityFunction1 and UtilityFunction2 should be public classes, since they need to have a constructor that populates the Dictionary (which involves variables, looping, and calculations).

On the other hand, I only need one of UtilityFunction1 and UtilityFunction2 , so it seems like they should be static methods. But if that's the case, how/when do they do the work to set up the Dictionaries they need?

How do you achieve this?

You may use a static constructor . A static constructor is guaranteed to be executed just once for the class and before the class is used for the very first time. A static constructor is a very good place for initialization of static properties. It may look like:

public class ExampleClass
{
    public static readonly IDictionary<int, Item> Items;

    static ExampleClass()
    {
        Items = new Dictionary<int, Item>();
    }
}

However, be careful with static stuff as it usually decreases the modularity and testability of a program. Therefore the use of a singleton pattern might be a better solution rather than a static constructor.

Maybe you can think of the Singelton Design Pattern for your UtilityFunction* https://msdn.microsoft.com/en-us/library/ff650316.aspx

You need a class that has only one instance, and you need to provide a global point of access to the instance.

You could also move your logic outside of Main. What forces you to use static is that Main method (and class it's part of, usually) is static. That forces all other things (fields, methods, events, you name it) to also be static. The easiest way of curing that headache would be to create new class to hold all things you need.

Create new Logic class and put all things in it. The Item class stays the same. you can move it to separate file for clarity.

Logic.cs:

public class Logic() {
    private Dictionary<object,object> dict1 = new Dictionary<object,object>
    private Dictionary<object,object> dict2 = new Dictionary<object,object>

    public Logic() {
        // some logical initializations here
    }

    private void UtilityFunction1 (Item itemToWorkOn) {
        // you can access dict1 directly from here
    }

    private void UtilityFunction2 (Item itemToWorkOn) {
        // you can access dict2 directly from here
    }

    public void Run() {
        // run UtilityFunction1() many times
        // run UtilityFunction2() many times
    }
}

Item.cs:

public class Item() {
    // a bunch of properties
    public Item() {}
}

And then just run it in your Main.

Program.cs:

static class Program {
    static void Main() {
        new Logic.Run();
    }
}

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