开发者

Initializing collections in Windows Form Applications

I am writing an application that will check text entered into a rich text box and see if it contains kanji (chinese characters) outside of a specific list.

I am currently using a string array for the 1000 kanji I want to allow and I'm just doing a for loop over each element in the user input and checking to see if the kanji is in the list or not. If it's not, I return a list at the end of all the 'not allowed' kanji.

My questions is, right now I am creating the string array inside the check method that is run when the user hits the 'check' button but I'm afraid that means I am creating the 1000 kanji list every time the operation is done. Where and how do I load the 1000 kanji string array so that it's alwa开发者_运维问答ys loaded into memory? It there any way to just initialize the pre-made list (I have no need to add or remove elements - I'm using it almost like a dictionary).


You're better of storing the characters in a List collection and use List.Contains to see if a character is on the list:

var myCharacters = new List<string>();
// Fill in the list...

if (myCharacters.Contains('?'))
{
  // Do something
}

Create and fill in 'myCharacters' list only once. Call myCharacters.Contains() method whenever you need to check for a character. Contains() method is optimized so it will be way more performant than your own implementation. Below is a full blown sample:

internal partial class MyForm : Form
{
    internal MyForm()
    {
        myCharacters.AddRange(...);
    }

    List<string> myCharacters = new List<string>();

    private void ValidateButton_Click(object sender, EventArgs e)
    {
        if (myCharacters.Contains('?'))
        {
            // Do something
        }
    }
}


Don't be afraid of initializing the array many times for performance reasons. 1000 items should not take much time at all.

That said, if you want to make sure it is only initialized once you should declare the list as a private member of your form and initialize the list in the constructor.

public partial class Form1 : Form
{
    private List<string> kanjiList;
    public Form1()
    {
        kanjiList = new List<string>{"a", "b", "c"};
    }
}

If you want to speed up lookups you can use HashSet<string> instead of List<string>, that will have the lookup performance comparable to a Dictionary.


It there any way to just initialize the pre-made list (I have no need to add or remove elements - I'm using it almost like a dictionary).

You can make your list variable static.

private static readonly string[] myList = new[] { "c1", "c2" };

This way it will be loaded once and will sit in the memory, as you want it to.

To search if user input contains a symbol from the list use can use this:

string input = "adscads";
bool found = myList.Any(input.Contains);

(Assuming .NET 3.5 or higher)


You will need to keep the list in a static field. That way you will have the list throughout your application.

class MyClass
{
    private static readonly List<char> _myCharList = new List<char>() { 'x', 'y', 'z' };
    public static List<char> MyCharList
    {
        get { return _myCharList; }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜