我有一个类,其中有一个字符串字段。这个字符串字段是常量,并且将来会移动到资源文件中,但目前它仍然存在于我们的类中。现在的情况是,我正在创建超过100个这个类的对象。因此,我的问题是采用哪种方法可以消耗更少的内存,为什么?
I should make my string variable
static
public class MyClass { public static string MyString = "My String"; }
I should make my string variable
const
public class MyClass { public const string MyString = "My String"; }
MyClass
对象实例,MyString
属性只会分配一次。这是使用static
的一个优点。 - Kevin Gosse