By default I use string constants in my code when I have a string of text that will be output to the screen via a messagebox, label, etc. My main reason for doing so is that I do not want these strings to change at runtime and this way nobody really touches them.
默认情况下,当我有一串文本将通过消息框,标签等输出到屏幕时,我在代码中使用字符串常量。我这样做的主要原因是我不希望这些字符串在运行时更改通过这种方式,没有人真正接触到它
My question is:
我的问题是:
It seems that from a memory point of view, this approach is creating an object whose lifetime will be decided by the GB whereas a string literal seems more efficient and will be disposed of more quickly. I know this is a rookie question but is there a benefit (no matter how tiny) when using a literal over a constant or should a constant be used in such scenarios.
似乎从内存的角度来看,这种方法是创建一个对象,其生命周期将由GB决定,而字符串文字似乎更有效并且将更快地处理。我知道这是一个新手问题,但是当使用文字超过常量或者在这种情况下应该使用常量时,是否有一个好处(无论多么微小)。
I know it will not make any noticeable difference in my apps but I am trying to improve myself as a dev and build standards that I keep a reasonable amount of the time. :o)
我知道它不会在我的应用程序中产生任何明显的差异,但我正在努力提高自己作为开发人员并建立标准,我保持合理的时间。 :O)
5 个解决方案
#1
Neither. The recommended way AFAIK is to use a resources file.
都不是。推荐的AFAIK方式是使用资源文件。
#2
Prefer string constants to string literals, but the reason is to make your code more maintainable, not to improve its performance.
首选字符串常量字符串文字,但原因是使代码更易于维护,而不是提高其性能。
#3
You should look into "string interning". Basically, all of the unique string literals (constant or inline) in any of your source files will be stored in a special table. They will never be garbage collected, and may benefit from re-use.
你应该看看“字符串实习”。基本上,任何源文件中的所有唯一字符串文字(常量或内联)都将存储在特殊表中。它们永远不会被垃圾收集,并可能从重复使用中受益。
You might want to look into using a resources file if you will ever be changing those strings without wanting to recompile (eg for internationalisation)
如果您想要更改这些字符串而不想重新编译(例如,用于国际化),您可能希望查看使用资源文件
#4
It would depend on whether you need these strings to be localizable or not. If you need the ability to translate your software into other languages, then I would place as many strings that are displayed to an end user as you can in resource files. If you do not need localization, or if the strings in question are never displayed to an end user, I would use constants. If your strings are shared amongst multiple classes, I would create classes that wrap up common strings that are provided as public constants. Unless these strings need to be used outside of the assembly they are defined in, keep them at the lowest privilege level possible (private, protected, internal).
这取决于您是否需要这些字符串可以本地化。如果您需要能够将软件翻译成其他语言,那么我会在资源文件中尽可能多地显示最终用户显示的字符串。如果您不需要本地化,或者有问题的字符串永远不会显示给最终用户,我会使用常量。如果您的字符串在多个类之间共享,我将创建包含作为公共常量提供的公共字符串的类。除非这些字符串需要在它们定义的程序集之外使用,否则请将它们保持在最低权限级别(私有,受保护,内部)。
#5
For internationalization purposes, it's true that the accepted best practice/recommendation is a set of resource files.
出于国际化目的,公认的最佳实践/建议是一组资源文件。
I am following the same pattern of using string constants in their own class. I find it's easier to create consts that'll be used on multiple pages throughout the web app. Even a const with placeholders helps!
我遵循在他们自己的类中使用字符串常量的相同模式。我发现创建将在整个Web应用程序的多个页面上使用的consts更容易。即使是带占位符的const也有帮助!
//{1} for the user's full name for example
const string SOME_LABEL = "Thanks for purchasing {0} products, {1}!";
#1
Neither. The recommended way AFAIK is to use a resources file.
都不是。推荐的AFAIK方式是使用资源文件。
#2
Prefer string constants to string literals, but the reason is to make your code more maintainable, not to improve its performance.
首选字符串常量字符串文字,但原因是使代码更易于维护,而不是提高其性能。
#3
You should look into "string interning". Basically, all of the unique string literals (constant or inline) in any of your source files will be stored in a special table. They will never be garbage collected, and may benefit from re-use.
你应该看看“字符串实习”。基本上,任何源文件中的所有唯一字符串文字(常量或内联)都将存储在特殊表中。它们永远不会被垃圾收集,并可能从重复使用中受益。
You might want to look into using a resources file if you will ever be changing those strings without wanting to recompile (eg for internationalisation)
如果您想要更改这些字符串而不想重新编译(例如,用于国际化),您可能希望查看使用资源文件
#4
It would depend on whether you need these strings to be localizable or not. If you need the ability to translate your software into other languages, then I would place as many strings that are displayed to an end user as you can in resource files. If you do not need localization, or if the strings in question are never displayed to an end user, I would use constants. If your strings are shared amongst multiple classes, I would create classes that wrap up common strings that are provided as public constants. Unless these strings need to be used outside of the assembly they are defined in, keep them at the lowest privilege level possible (private, protected, internal).
这取决于您是否需要这些字符串可以本地化。如果您需要能够将软件翻译成其他语言,那么我会在资源文件中尽可能多地显示最终用户显示的字符串。如果您不需要本地化,或者有问题的字符串永远不会显示给最终用户,我会使用常量。如果您的字符串在多个类之间共享,我将创建包含作为公共常量提供的公共字符串的类。除非这些字符串需要在它们定义的程序集之外使用,否则请将它们保持在最低权限级别(私有,受保护,内部)。
#5
For internationalization purposes, it's true that the accepted best practice/recommendation is a set of resource files.
出于国际化目的,公认的最佳实践/建议是一组资源文件。
I am following the same pattern of using string constants in their own class. I find it's easier to create consts that'll be used on multiple pages throughout the web app. Even a const with placeholders helps!
我遵循在他们自己的类中使用字符串常量的相同模式。我发现创建将在整个Web应用程序的多个页面上使用的consts更容易。即使是带占位符的const也有帮助!
//{1} for the user's full name for example
const string SOME_LABEL = "Thanks for purchasing {0} products, {1}!";