c# - variable - what is public const string




Declaring strings public static readonly versus public const versus public static const (4)

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?


Given that you're using Visual Studio, a small benefit for using public const over public static readonly is the ability to use IntelliSense to "peak" at the const value.

e.g. Given:

public class Constants
{
     public const string ConstString = "Visible!";
     public static readonly string StaticReadonlyString = "Invisible!";
}


Speedwise the difference between public static string and public const string is negligible.

In IL it's a difference between

ldsfld someaddr.field 

and

ldstr "your const here"

Here lies the answer though. By using const you're literally make your assembly to use that literal every time you use it, so the assembly will be ridden with those literals. With static the will be "pointers" to the central location.

The biggest gotcha lies in the following: you can't perfrom switch case against static strings, but you can against const (as you should).

So, my take on this: if you need to use switch - you have to use use const; if you don't - I'd prolly go with static.

HTH

Nikolai


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.





string