只读声明字符串公共静态与公共常量与公共静态常量 [英] Declaring strings public static readonly versus public const versus public static const

查看:305
本文介绍了只读声明字符串公共静态与公共常量与公共静态常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每一个项目,我们有没有用于存储在该项目中使用的各种SQL语句的文件。 。有变化对类是如何声明一把弦是如何声明



Example类declartions:

 内部密封类类名
内部静态className类
公共密封类类名
公共静态className类
内部类类名

示例字符串声明:

 内部常量字符串字符串名称
内部静态字符串字符串名称
公共静态只读字符串字符串名称
公共静态字符串字符串名称
公共常量字符串字符串名称

我不明白,对性能的影响是不同的声明之间是什么。是否有这种情况/方案中的最佳做法?


解决方案

我不明白,对性能的影响是不同的声明之间有什么




评估数据库查询很可能将成为数以百万计的成本十亿次从恒定改变到只读字段或反之亦然的成本差异。甚至不要担心的事情是性能需要对夫妇,当你有一个有延迟以毫秒为单位的数据库操作纳秒。



什么你应该担心的是语义,而不是性能。现在的问题归结为只读,常量或都不是?



获取的语义正确的。 A只读字段中的意思是这个领域的变化正是每一次,这个时间执行程序,从零到它的价值。 A常量字段的含义是值永远不会改变,而不是现在,不是在未来的版本,而不是永远,是的常数所有时间。一个普通的字段可以随时更改值。



一个只读域是像一个版本号。随时间变化,但在程序的执行不会改变。常量是像圆周率,或者铅的原子序数;它是固定的,永恒的,永远不会改变。一个普通的字段是很好的东西,改变了该方案的过程中,像黄金价格。这是您的查询什么样的?这将是整个计划的过程中不断的,不断的为所有的时间,或不固定呢?


In each project we have there is a file used to store the various SQL statements used in that project. There are a handful of variations on how the class is declared and how the strings are declared.

Example class declartions:

internal sealed class ClassName
internal static class ClassName
public sealed class ClassName
public static class ClassName
internal class ClassName

Example string declarations:

internal const string stringName
internal static string stringName
public static readonly string stringName
public static string stringName
public const string stringName

I don't understand what the performance implications are between the different declarations. Is there a best practice for this situation/scenario?

解决方案

I don't understand what the performance implications are between the different declarations

The cost of evaluating the database query is probably going to be millions or billions of times the cost difference of changing from a constant to a readonly field or vice versa. Don't even worry about performance of something that takes a couple of nanoseconds when you have database operations that have latency measured in milliseconds.

What you should be worrying about is semantics, not performance. The question boils down to "readonly, constant or neither?"

Get the semantics right. A "readonly" field means "this field changes exactly once per time this program is executed", from null to its value. A "const" field means "this value never changes, not now, not in the next version, not ever, it is constant for all time." An ordinary field can change value at any time.

A readonly field is something like a version number. It changes over time, but does not change over the execution of the program. A constant is something like pi, or the atomic number of lead; it is fixed, eternal, never changes. An ordinary field is good for something that changes over the course of the program, like the price of gold. Which is your query like? Will it be constant throughout the course of this program, constant for all time, or not constant at all?

这篇关于只读声明字符串公共静态与公共常量与公共静态常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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