在给定的情况下,我应该在哪里初始化集合? [英] where should i initialize collections in the given scenario?

查看:81
本文介绍了在给定的情况下,我应该在哪里初始化集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字典

Dictionary<string, string> Keywords = new Dictionary<string, string>();
Keywords.Add("start", "START");
Keywords.Add("int", "DT");
Keywords.Add("real", "DT");
.
.




该函数在一个类中,如果我在其中定义字典,则每次调用都需要花费很多时间.所以我应该在哪里初始化它?如果我在main()中做到这一点,那我该如何在班级中调用它呢?




this function is in a class and call many times if i define dictionary in it, every time it takes time. so where should i initialize it? if i do this in main() then how can i call it in my class?

public string IsKeyword(string value)
{ 
}

推荐答案

在类级别定义字典.
Define the dictionary at the class level.
class myClass
{
  Dictionary<string,> Keywords = new Dictionary<string,>();

  public void DictionaryInitializer()
  {
     Keywords.Clear();
     Keywords.Add("start", "START");
     Keywords.Add("int", "DT");
     Keywords.Add("real", "DT");
  }

}


当然,Abhinav和SAK的明智评论都为您指明了正确的方向,但让我补充一个补充……也许是多余的..评论:

初始化字典的位置取决于其在项目总体方案"中的战略用途.

1.如果Dictionary对象是被实例化很多次的类的一部分,并且您的意图该类的每个实例都具有该Dictionary的私有实例,则实例化显然属于该类.

2.如果Dictionary对象是许多类(窗体等)的全局资源,并且是不变的,则可以考虑将其放在静态类中,仅公开希望的访问方法等等.或者,可以在"Singleton"类中定义字典.

最好,Bill
Of course both Abhinav''s and SAK''s wise comments point you in the right direction, but let me add one supplementary ... perhaps superfluous ... comment:

Where you initialize the Dictionary depends on its strategic use in the ''total scenario'' in your project.

1. if the Dictionary object is part of a class that is instantiated many times, and it is your intent that each instance of the class has a private instance of this Dictionary, obviously the instantiation belongs in the class.

2. if the Dictionary object is a global resource for many classes (forms, whatever), and is invariant, you can consider putting it in a static class, exposing only those methods of access to it you wish, etc. Or, the Dictionary could be defined in a ''Singleton'' Class.

best, Bill


假设关键字是常量,那么在类级别定义该集合就更有意义了.先前的解决方案已经提到了这一点.我只想补充一点,因为C#4(或3?)可以直接初始化集合(如上文中sa所述).


一个完整的类将如下所示:

Assuming that the keywords are constant, then it makes more sense that the collection is defined at the class level. The previous solutions have already mentioned that. I just want to add that since C#4 (or 3?) it''s possible to initialize the collections directly (as sa has already mentioned above).


a complete class would then look as follows:

static class myClass
{
    static Dictionary<string, string> Keywords = new Dictionary<string, string>() 
        {   
            {"start", "START" },
            { "int", "DT" },
            {"real", "DT"}
        };

    public static string IsKeyword(string value)
    { 
        return "???";
    }
}


我对IsKeyword 方法的签名有些困惑.对于作为类用户的我来说,没有Is...方法返回字符串是没有意义的,这就是为什么我将实现留空的原因.


I''m a bit confused about the signature of the IsKeyword method. for me as a user of the class it doesn''t make sense to have a Is... method that returns a string, that''s why I left the implementation empty.


这篇关于在给定的情况下,我应该在哪里初始化集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆