Static string interpolation












1















public static class SomeRegexConsts
{
public static readonly string FullName = $"{Name} {Surname}";

private static readonly string Name = "[A-Z][a-z]+";

private static readonly string Surname = "[A-Z][a-z]+";
}


In above example FullName will be equal to " " at runtime.
It's related to the way static fields are initialized (from top to bottom).



In general I don't see any other solution than:



public static string FullName => $"{Name} {Surname}";


Any ideas how to improve this code as I don't like this lambda, and moving FullName below Name and Surname is not an option as StyleCop won't let me do this.










share|improve this question




















  • 1





    I hope this is just a simplified example, because those regex patterns won't match names in the real world.

    – Ben Voigt
    Jan 2 at 14:53











  • Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question

    – Random Guy
    Jan 2 at 14:55













  • You do realize that there are names that have letters outside the range of A-Z?

    – Rand Random
    Jan 2 at 15:03











  • Yes I do, but probably you do not realize that I've already answered this question :)

    – Random Guy
    Jan 2 at 15:06











  • Who reads comments? Sorry, about asking the same question again. :)

    – Rand Random
    Jan 2 at 15:07


















1















public static class SomeRegexConsts
{
public static readonly string FullName = $"{Name} {Surname}";

private static readonly string Name = "[A-Z][a-z]+";

private static readonly string Surname = "[A-Z][a-z]+";
}


In above example FullName will be equal to " " at runtime.
It's related to the way static fields are initialized (from top to bottom).



In general I don't see any other solution than:



public static string FullName => $"{Name} {Surname}";


Any ideas how to improve this code as I don't like this lambda, and moving FullName below Name and Surname is not an option as StyleCop won't let me do this.










share|improve this question




















  • 1





    I hope this is just a simplified example, because those regex patterns won't match names in the real world.

    – Ben Voigt
    Jan 2 at 14:53











  • Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question

    – Random Guy
    Jan 2 at 14:55













  • You do realize that there are names that have letters outside the range of A-Z?

    – Rand Random
    Jan 2 at 15:03











  • Yes I do, but probably you do not realize that I've already answered this question :)

    – Random Guy
    Jan 2 at 15:06











  • Who reads comments? Sorry, about asking the same question again. :)

    – Rand Random
    Jan 2 at 15:07
















1












1








1








public static class SomeRegexConsts
{
public static readonly string FullName = $"{Name} {Surname}";

private static readonly string Name = "[A-Z][a-z]+";

private static readonly string Surname = "[A-Z][a-z]+";
}


In above example FullName will be equal to " " at runtime.
It's related to the way static fields are initialized (from top to bottom).



In general I don't see any other solution than:



public static string FullName => $"{Name} {Surname}";


Any ideas how to improve this code as I don't like this lambda, and moving FullName below Name and Surname is not an option as StyleCop won't let me do this.










share|improve this question
















public static class SomeRegexConsts
{
public static readonly string FullName = $"{Name} {Surname}";

private static readonly string Name = "[A-Z][a-z]+";

private static readonly string Surname = "[A-Z][a-z]+";
}


In above example FullName will be equal to " " at runtime.
It's related to the way static fields are initialized (from top to bottom).



In general I don't see any other solution than:



public static string FullName => $"{Name} {Surname}";


Any ideas how to improve this code as I don't like this lambda, and moving FullName below Name and Surname is not an option as StyleCop won't let me do this.







c# string static interpolation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 15:10









Rand Random

3,09873164




3,09873164










asked Jan 2 at 14:36









Random GuyRandom Guy

85




85








  • 1





    I hope this is just a simplified example, because those regex patterns won't match names in the real world.

    – Ben Voigt
    Jan 2 at 14:53











  • Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question

    – Random Guy
    Jan 2 at 14:55













  • You do realize that there are names that have letters outside the range of A-Z?

    – Rand Random
    Jan 2 at 15:03











  • Yes I do, but probably you do not realize that I've already answered this question :)

    – Random Guy
    Jan 2 at 15:06











  • Who reads comments? Sorry, about asking the same question again. :)

    – Rand Random
    Jan 2 at 15:07
















  • 1





    I hope this is just a simplified example, because those regex patterns won't match names in the real world.

    – Ben Voigt
    Jan 2 at 14:53











  • Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question

    – Random Guy
    Jan 2 at 14:55













  • You do realize that there are names that have letters outside the range of A-Z?

    – Rand Random
    Jan 2 at 15:03











  • Yes I do, but probably you do not realize that I've already answered this question :)

    – Random Guy
    Jan 2 at 15:06











  • Who reads comments? Sorry, about asking the same question again. :)

    – Rand Random
    Jan 2 at 15:07










1




1





I hope this is just a simplified example, because those regex patterns won't match names in the real world.

– Ben Voigt
Jan 2 at 14:53





I hope this is just a simplified example, because those regex patterns won't match names in the real world.

– Ben Voigt
Jan 2 at 14:53













Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question

– Random Guy
Jan 2 at 14:55







Oh, yes, it is simplified. Actual regex is way longer but it is not a topic of this question

– Random Guy
Jan 2 at 14:55















You do realize that there are names that have letters outside the range of A-Z?

– Rand Random
Jan 2 at 15:03





You do realize that there are names that have letters outside the range of A-Z?

– Rand Random
Jan 2 at 15:03













Yes I do, but probably you do not realize that I've already answered this question :)

– Random Guy
Jan 2 at 15:06





Yes I do, but probably you do not realize that I've already answered this question :)

– Random Guy
Jan 2 at 15:06













Who reads comments? Sorry, about asking the same question again. :)

– Rand Random
Jan 2 at 15:07







Who reads comments? Sorry, about asking the same question again. :)

– Rand Random
Jan 2 at 15:07














3 Answers
3






active

oldest

votes


















2














If you don't want to move FullName below others, I think this's only solution for you.



public static class SomeRegexConsts
{
static SomeRegexConsts(){
FullName = $"{Name} {Surname}";
}

public static readonly string FullName;

private static readonly string Name = "[A-Z][a-z]+";

private static readonly string Surname = "[A-Z][a-z]+";





share|improve this answer



















  • 1





    Looks good to me. I've totally forgot about static ctors :)

    – Random Guy
    Jan 2 at 14:47



















0














What about creating a public method that returns the FullName string ?



public string getFullname()
{
return $"{Name} {Surname}";
}


This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.






share|improve this answer
























  • yes, but it adds another method invocation for something that won't change

    – Random Guy
    Jan 2 at 14:50



















0














I believe this would work with StyleCop since it requires const before fields of any type:



public static class SomeRegexConsts
{
private const string Name = "[A-Z][a-z]+";
private const string Surname = "[A-Z][a-z]+";

public static readonly string FullName = $"{Name} {Surname}";

}


unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008215%2fstatic-string-interpolation%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    If you don't want to move FullName below others, I think this's only solution for you.



    public static class SomeRegexConsts
    {
    static SomeRegexConsts(){
    FullName = $"{Name} {Surname}";
    }

    public static readonly string FullName;

    private static readonly string Name = "[A-Z][a-z]+";

    private static readonly string Surname = "[A-Z][a-z]+";





    share|improve this answer



















    • 1





      Looks good to me. I've totally forgot about static ctors :)

      – Random Guy
      Jan 2 at 14:47
















    2














    If you don't want to move FullName below others, I think this's only solution for you.



    public static class SomeRegexConsts
    {
    static SomeRegexConsts(){
    FullName = $"{Name} {Surname}";
    }

    public static readonly string FullName;

    private static readonly string Name = "[A-Z][a-z]+";

    private static readonly string Surname = "[A-Z][a-z]+";





    share|improve this answer



















    • 1





      Looks good to me. I've totally forgot about static ctors :)

      – Random Guy
      Jan 2 at 14:47














    2












    2








    2







    If you don't want to move FullName below others, I think this's only solution for you.



    public static class SomeRegexConsts
    {
    static SomeRegexConsts(){
    FullName = $"{Name} {Surname}";
    }

    public static readonly string FullName;

    private static readonly string Name = "[A-Z][a-z]+";

    private static readonly string Surname = "[A-Z][a-z]+";





    share|improve this answer













    If you don't want to move FullName below others, I think this's only solution for you.



    public static class SomeRegexConsts
    {
    static SomeRegexConsts(){
    FullName = $"{Name} {Surname}";
    }

    public static readonly string FullName;

    private static readonly string Name = "[A-Z][a-z]+";

    private static readonly string Surname = "[A-Z][a-z]+";






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 14:42









    Antoine VAntoine V

    5,1142425




    5,1142425








    • 1





      Looks good to me. I've totally forgot about static ctors :)

      – Random Guy
      Jan 2 at 14:47














    • 1





      Looks good to me. I've totally forgot about static ctors :)

      – Random Guy
      Jan 2 at 14:47








    1




    1





    Looks good to me. I've totally forgot about static ctors :)

    – Random Guy
    Jan 2 at 14:47





    Looks good to me. I've totally forgot about static ctors :)

    – Random Guy
    Jan 2 at 14:47













    0














    What about creating a public method that returns the FullName string ?



    public string getFullname()
    {
    return $"{Name} {Surname}";
    }


    This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.






    share|improve this answer
























    • yes, but it adds another method invocation for something that won't change

      – Random Guy
      Jan 2 at 14:50
















    0














    What about creating a public method that returns the FullName string ?



    public string getFullname()
    {
    return $"{Name} {Surname}";
    }


    This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.






    share|improve this answer
























    • yes, but it adds another method invocation for something that won't change

      – Random Guy
      Jan 2 at 14:50














    0












    0








    0







    What about creating a public method that returns the FullName string ?



    public string getFullname()
    {
    return $"{Name} {Surname}";
    }


    This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.






    share|improve this answer













    What about creating a public method that returns the FullName string ?



    public string getFullname()
    {
    return $"{Name} {Surname}";
    }


    This way public static readonly string FullName = $"{Name} {Surname}";, if it's ok to get rid of it, won't be needed anymore.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 14:43









    Noob devNoob dev

    869




    869













    • yes, but it adds another method invocation for something that won't change

      – Random Guy
      Jan 2 at 14:50



















    • yes, but it adds another method invocation for something that won't change

      – Random Guy
      Jan 2 at 14:50

















    yes, but it adds another method invocation for something that won't change

    – Random Guy
    Jan 2 at 14:50





    yes, but it adds another method invocation for something that won't change

    – Random Guy
    Jan 2 at 14:50











    0














    I believe this would work with StyleCop since it requires const before fields of any type:



    public static class SomeRegexConsts
    {
    private const string Name = "[A-Z][a-z]+";
    private const string Surname = "[A-Z][a-z]+";

    public static readonly string FullName = $"{Name} {Surname}";

    }


    unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.






    share|improve this answer




























      0














      I believe this would work with StyleCop since it requires const before fields of any type:



      public static class SomeRegexConsts
      {
      private const string Name = "[A-Z][a-z]+";
      private const string Surname = "[A-Z][a-z]+";

      public static readonly string FullName = $"{Name} {Surname}";

      }


      unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.






      share|improve this answer


























        0












        0








        0







        I believe this would work with StyleCop since it requires const before fields of any type:



        public static class SomeRegexConsts
        {
        private const string Name = "[A-Z][a-z]+";
        private const string Surname = "[A-Z][a-z]+";

        public static readonly string FullName = $"{Name} {Surname}";

        }


        unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.






        share|improve this answer













        I believe this would work with StyleCop since it requires const before fields of any type:



        public static class SomeRegexConsts
        {
        private const string Name = "[A-Z][a-z]+";
        private const string Surname = "[A-Z][a-z]+";

        public static readonly string FullName = $"{Name} {Surname}";

        }


        unless you're using reflection within the class that requires a non-const field, the compiled code should be unaffected.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 15:16









        D StanleyD Stanley

        124k9116180




        124k9116180






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008215%2fstatic-string-interpolation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'