Static code blocks

C#.NetStatic

C# Problem Overview


Going from Java to C# I have the following question: In java I could do the following:

public class Application {
    static int attribute;
    static {
        attribute = 5;
    }
   // ... rest of code
}

I know I can initialize this from the constructor but this does not fit my needs (I want to initialize and call some utility functions without create the object). Does C# support this? If yes, how can I get this done?

Thanks in advance,

C# Solutions


Solution 1 - C#

public class Application
{     

    static int attribute;     
    static Application()
    {         
         attribute = 5;     
    }    // removed
}

You can use the C# equivalent static constructors. Please don't confuse it with a regular constructor. A regular constructor doesn't have a static modifier in front of it.

I am assuming your //... rest of the code need to be also run once. If you don't have such code you can just simply do this.

 public class Application
 {     

    static int attribute = 5;
 }

Solution 2 - C#

You just can write a static constructor block like this,

static Application(){
 attribute=5;
}

This is what I could think of.

Solution 3 - C#

In your particular scenario, you could do the following:

public class Application { 
    static int attribute = 5;
   // ... rest of code 
}

UPDATE:

It sounds like you want to call a static method. You can do that as follows:

public static class Application {
    static int attribute = 5;

    public static int UtilityMethod(int x) {
        return x + attribute;
    }
}

Solution 4 - C#

-A static constructor doesn't have any parameter.
-A static class can contain only one static constructor.
-A static constructor executes first when we run the program.

Example:

namespace InterviewPreparation  
{  
    public static class Program  
    {  //static Class
        static Program()  
        { //Static constructor
            Console.WriteLine("This is static consturctor.");  
        }  
        public static void Main()  
        { //static main method
            Console.WriteLine("This is main function.");  
            Console.ReadKey();  
        }  
    }  
}  

Output:
This is static constructor.
This is main function.

Solution 5 - C#

I find something else useful. If your variable needs more than one expressions/statements to initialize, use this!

static A a = new Func<A>(() => {
   // do it here
   return new A();
})();

This approach is not limited on classes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGETahView Question on Stackoverflow
Solution 1 - C#parapura rajkumarView Answer on Stackoverflow
Solution 2 - C#AjaiView Answer on Stackoverflow
Solution 3 - C#Phil KleinView Answer on Stackoverflow
Solution 4 - C#Vikas ChaturvediView Answer on Stackoverflow
Solution 5 - C#ice1000View Answer on Stackoverflow