Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring strings public static readonly versus public const versus public static const

Tags:

string

c#

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?

like image 491
Ryan Rodemoyer Avatar asked Dec 09 '10 20:12

Ryan Rodemoyer


People also ask

What is difference between static const and read only?

The first, const, is initialized during compile-time and the latter, readonly, initialized is by the latest run-time. The second difference is that readonly can only be initialized at the class-level. Another important difference is that const variables can be referenced through "ClassName.

What is the difference between the static const and readonly keyword when applied to a type member?

The difference lies in the details. First, a const field is not a reference to anything; it is literal value "burned" into the code (using a const is the true definition of hard coding a value). A static readonly variable is a reference, and consequently a lookup is performed any time this variable is accessed.

What is difference between static constant and readonly variables?

Difference between const and readonlyconst variables can declared in methods ,while readonly fields cannot be declared in methods. const fields cannot be used with static modifier, while readonly fields can be used with static modifier.

What is difference between const and static in C#?

const is a constant value, and cannot be changed. It is compiled into the assembly. static means that it is a value not related to an instance, and it can be changed at run-time (since it isn't readonly ). So if the values are never changed, use consts.


2 Answers

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?

like image 174
Eric Lippert Avatar answered Oct 07 '22 19:10

Eric Lippert


You should choose an access modifier (public or internal) based on which code uses the strings.

  • static const is a compiler error.

  • A static readonly field is a normal field which cannot be set after the static ctor.

  • A const string will be replaced by its literal value at compile time.
    Therefore, it will give slightly better performance (since the runtime doesn't use the field).
    However, since it's substituted at compile-time, any changes to the definition in one assembly will not be picked up by other assemblies until they're all recompiled.

If your code is only used in one assembly, you might as well use const strings.
However, if your strings are used by other assemblies (or might be in the future), you should use static readonly strings for maintainability.

Also note that a const string is a compile-time constant.
If you need to incldue things like the machine name or username in the string, you need to make it static readonly.

like image 24
SLaks Avatar answered Oct 07 '22 21:10

SLaks