0

I have two constants, defined in the same static class:

public static class MyConstants {
    public const string ABC = "ABC";
    public const string DEF = "DEF";
}

I use them in a combined manner, elsewhere in my application:

Console.WriteLine(MyConstants.ABC + MyConstants.DEF);

I'm trying to determine if the concatenation is optimized at compile time, resulting in the assembly containing a value of ABCDEF at some address when disassembled, or if it stays separate. I followed the instructions on this MSDN post to view the disassembly of my code in a console application and it appears that it is not stored as ABCDEF, nor separate as ABC, DEF:

Disassembly Window

This post was a good read, but ultimately confused me in relation to my question (likely due to my lack of C++ knowledge). I've looked at several other posts and don't really seem to be getting anywhere near the answer.


Does C# optimize the concatenation of two constants in the compiled version of an application, any/everywhere that concatenation is used?

NOTE: The goal is to further obfuscate the full value of ABCDEF, making it harder to find.

Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44
  • Take a look at [this](https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQxASwDYB8ACAmARgFgAoHAZgAJ9KBhSgb1MpcoGMB7AO1moIAZKAZRgI0XAOYFKAXkoAiAIIAhWvIDczVpx4w+gkWMl5ZCgCIBRAGIatLCtQAslALIAKAJSM7rewMoIcFAArhh6cobiUgDUkcaaJL6+OAQAnG6BIWEeCb4AvqR5QA==) and note the instruction `ldstr "ABCDEF"`. – madreflection Apr 08 '21 at 16:31
  • @madreflection, always around to help lol I should really bookmark that website haha, so that shows me that it was indeed optimized and can be found as `ABCDEF` in the disassembly? – Hazel へいぜる Apr 08 '21 at 16:32
  • Yes, that's correct. – madreflection Apr 08 '21 at 16:33
  • As far as I know, the concatenation of constants results in a compile-time constant. That's why you can use an expression like `MyConstants.ABC + MyConstants.DEF` in places where a constant is required (for example, within Attributes, or as default values for parameters). It's the same idea with `const int`s (they add up at compile time) – Flydog57 Apr 08 '21 at 16:34
  • 3
    The most important sentence in your question is the note at the end. **Do not attempt this sort of security through obscurity**. If you have a secret in your code that must be kept secret *from the person running the code* then you have failed to build a correct threat model and you should stop writing code until you have built a correct threat model. The .NET security system is explicitly designed to protect *benign users* from *hostile code*; your note implies that you wish to protect *benign code* from *hostile users*, and you will fail to do so. – Eric Lippert Apr 08 '21 at 17:24
  • 1
    @EricLippert thank you for pointing that out! I'd never thought of it that way! – Hazel へいぜる Apr 08 '21 at 18:00
  • 1
    @タコス: You might want to use [`SecureString`](https://learn.microsoft.com/en-us/dotnet/api/system.security.securestring?view=net-5.0) – Tim Schmelter Apr 08 '21 at 18:57
  • @TimSchmelter thanks for that link! It's a good reference point, but note the remark that it shouldn't be used for new development about half way down the page. Thanks again for the reading material though! – Hazel へいぜる Apr 08 '21 at 19:17

1 Answers1

0

Yes, the concatenated string is interned see: .NET-Fiddle

public class Program
{
    public static void Main()
    {
        string abcdef = MyConstants.ABC + MyConstants.DEF;
         if(string.IsInterned(abcdef) != null)
        {
            Console.Write("abcdef is interned");
        }
    }
}


public static class MyConstants {
    public const string ABC = "ABC";
    public const string DEF = "DEF";
}
 

I think Jon answers it also here: https://stackoverflow.com/a/288802/284240

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939