Existance of a Static variable.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Duggi

    Existance of a Static variable.

    Hi

    When does a static variable come into life? Well, as far as my
    knowledge goes, I think it is during the execution of static
    constructor. Because thats the logic point in the program where the
    usage of static variable starts.

    however from the following program sample, I see that static variables
    will come into existence even before the static constructor gets
    invoked.

    public class A
    {
    public static B abc = new B("from decalration");

    static A()
    {
    abc = new B("From Constructor");
    }

    }

    public class B
    {
    public B(string message)
    {
    Console.WriteLi ne(message);
    }
    }

    public class TestPrg
    {
    public static void Main()
    {
    A a = new A();

    Console.ReadLin e();
    }
    }


    Please let me know if I am wrong or your view on it.

    -Cnu
  • Jon Skeet [C# MVP]

    #2
    Re: Existance of a Static variable.

    Duggi <DuggiSrinivasa Rao@gmail.comwr ote:
    Thanks for pulling out the specification from ???

    75351c669b09/CSharp%20Langua ge%20Specificat ion.doc
    If a static constructor (§10.12)
    exists in the class, execution of the static field initializers occurs
    immediately prior to executing that static constructor.
    So a static variable will come into existance even before the static
    constructor gets the control ?
    Again, it's not a case of when the variable "comes into existence" -
    it's when the initializer is executed. And yes, the initializer is
    executed before the static constructor is executed.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Duggi

      #3
      Re: Existance of a Static variable.

      On Sep 22, 6:11 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
      Duggi <DuggiSrinivasa ...@gmail.comwr ote:
      Thanks for pulling out the specification from ???
      >

      75351c669b09/CSharp%20Langua ge%20Specificat ion.doc
      >
      If a static constructor (§10.12)
      exists in the class, execution of the static field initializers occurs
      immediately prior to executing that static constructor.
      >
      So a static variable will come into existance even before the static
      constructor gets the control ?
      >
      Again, it's not a case of when the variable "comes into existence" -
      it's when the initializer is executed. And yes, the initializer is
      executed before the static constructor is executed.
      >
      --
      Jon Skeet - <sk...@pobox.co m>
      Web site:http://www.pobox.com/~skeet 
      Blog:http://www.msmvps.com/jon.skeet
      C# in Depth:http://csharpindepth.com
      Thanks Jon.

      Is there any difference between, "comes into existence" and execution
      of initializer??

      -Cnu

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Existance of a Static variable.

        Duggi <DuggiSrinivasa Rao@gmail.comwr ote:
        Is there any difference between, "comes into existence" and execution
        of initializer??
        Yes. All the variables effectively "come into existence" at the same
        time, before any of the initializers are executed. Consider the
        following program:

        public class Test
        {
        static int first = SumFirstAndSeco nd();
        static int second = SumFirstAndSeco nd();

        private static int SumFirstAndSeco nd()
        {
        return first + second;
        }

        static void Main(string[] args)
        {
        // No-op
        }
        }

        The initializer for first calls SumFirstAndSeco nd, which uses both
        first and second, so both variables must *exist* (and be addressable)
        at that point. However, the initializer for first is still executing
        and the initializer for second hasn't even begun executing at that
        point.

        --
        Jon Skeet - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        Working...