0

I have quite a sizeable static constructor for one of my classes, and was wanting to refactor it to encapsulate various bits of the initialization within static functions.

One of the things this static constructor does a lot of is initialize the values of static readonly fields. However, when I try to move these parts into functions, obviously the compiler wont let me as these can only be set in the static constructor. This makes sense as it doesn't know that I only intend to call these functions from my static constructor.

Is there any way around this? e.g. is there some sort of attribute I can put on the functions to tell the compiler the functions are only to be called from the static constructor?

0

2 Answers 2

5

You can define local functions inside a static constructor;

public class MyClass {
    public static readonly int Value;
    
    static MyClass(){
        Value = Calc(1);
        
        return;
        
        int Calc(int a) => a;
    }
}

The C# compiler will turn those local functions into static methods, with unpronounceable names.

1
  • This isn't exactly what I asked for, but it looks like the conceptually closest I'll get if that doesn't exist.
    – fweaks
    Commented Apr 6, 2022 at 2:37
0

You can do it with static and private keywords

using System;

public class User {
    public static readonly string Name;
    
    static User(){
        Name = GetName();
    }
    
    //`private` function to only be called in static class itself
    private static string GetName() {
       return "Testing";
    }
}

public class Program
{
    public static void Main()
    {
        Console.WriteLine(User.Name);
    }
}

A side note for this solution is that it can help you to prevent outsider functions' calls but cannot prevent being called internally by inner functions.

5
  • 1
    This does reduce the risk of the GetName function getting called in an undesired way, but it does not guarantee it wont can't be called outside the constructor. It could be called within the scope of the User class, in a random function.
    – Cleptus
    Commented Apr 1, 2022 at 6:25
  • Yes, indeed. Let me mention that in my answer too. Thanks for your feedback!
    – Nick Vu
    Commented Apr 1, 2022 at 6:32
  • 1
    Note that static doesn't do anything to prevent access from elsewhere... that's what the private part is for. static is just about the method being related to the type rather than a specific instance of the type.
    – Jon Skeet
    Commented Apr 1, 2022 at 6:37
  • @JonSkeet Wow, I read your book C# in depth. It's one of my favorite books. By the way, your feedback is true :D
    – Nick Vu
    Commented Apr 1, 2022 at 6:44
  • This is still doing the actual assignment of the static readonly field directly in the static constructor, which is what I'm trying to avoid. I have a lot of these that I'd like to encapsulate in a single separate function, to make the static constructor itself more readable.
    – fweaks
    Commented Apr 6, 2022 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.