Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Static Values

Static Values

Scheduled Pinned Locked Moved C#
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrEyes
    wrote on last edited by
    #1

    Hello all I was under the impression that if I make a call into a static function which uses the value of a static class variable, if I recall the function later the value of the static class variable would reset back to the default coded value. Now as the following example prooves I am obviously wrong. I have a test application/dll that has the following code : StaticLib.dll

    using System;

    namespace StaticLib
    {
    public class MyStaticLib
    {
    private static int val = 0;
    public static int GetNumber()
    {
    return val++;
    }
    }
    }

    Console Application (which references StaticLib.dll)

    using System;
    using StaticLib;
    using System.Threading;

    namespace StaticTest
    {
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    while(true)
    {
    Console.WriteLine(MyStaticLib.GetNumber());
    Thread.Sleep(1000);
    }
    }
    }
    }

    When I ran this for the first time I expected to get the following output

    0
    0
    0
    0
    0
    0

    But I actually get

    1
    2
    3
    4
    5
    6
    7

    Could anybody explain why this is? post.mode = postmodes.signature; SELECT everything FROM everywhere WHERE something = something_else; > 1 Row Returned > 42

    C R 2 Replies Last reply
    0
    • M MrEyes

      Hello all I was under the impression that if I make a call into a static function which uses the value of a static class variable, if I recall the function later the value of the static class variable would reset back to the default coded value. Now as the following example prooves I am obviously wrong. I have a test application/dll that has the following code : StaticLib.dll

      using System;

      namespace StaticLib
      {
      public class MyStaticLib
      {
      private static int val = 0;
      public static int GetNumber()
      {
      return val++;
      }
      }
      }

      Console Application (which references StaticLib.dll)

      using System;
      using StaticLib;
      using System.Threading;

      namespace StaticTest
      {
      class Class1
      {
      [STAThread]
      static void Main(string[] args)
      {
      while(true)
      {
      Console.WriteLine(MyStaticLib.GetNumber());
      Thread.Sleep(1000);
      }
      }
      }
      }

      When I ran this for the first time I expected to get the following output

      0
      0
      0
      0
      0
      0

      But I actually get

      1
      2
      3
      4
      5
      6
      7

      Could anybody explain why this is? post.mode = postmodes.signature; SELECT everything FROM everywhere WHERE something = something_else; > 1 Row Returned > 42

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      MrEyes wrote: I recall the function later the value of the static class variable would reset back to the default coded value. That is not correct. The value will not reset unless you have code to explicitly reset it. MrEyes wrote: Could anybody explain why this is? That is the correct behaviour. A static is a value that exists for all instances of the class. If some code modifies it then all instances of the class (and any static methods/properties) see the modification. Some people think of a static that is a global value within the class. Does this help?


      My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

      1 Reply Last reply
      0
      • M MrEyes

        Hello all I was under the impression that if I make a call into a static function which uses the value of a static class variable, if I recall the function later the value of the static class variable would reset back to the default coded value. Now as the following example prooves I am obviously wrong. I have a test application/dll that has the following code : StaticLib.dll

        using System;

        namespace StaticLib
        {
        public class MyStaticLib
        {
        private static int val = 0;
        public static int GetNumber()
        {
        return val++;
        }
        }
        }

        Console Application (which references StaticLib.dll)

        using System;
        using StaticLib;
        using System.Threading;

        namespace StaticTest
        {
        class Class1
        {
        [STAThread]
        static void Main(string[] args)
        {
        while(true)
        {
        Console.WriteLine(MyStaticLib.GetNumber());
        Thread.Sleep(1000);
        }
        }
        }
        }

        When I ran this for the first time I expected to get the following output

        0
        0
        0
        0
        0
        0

        But I actually get

        1
        2
        3
        4
        5
        6
        7

        Could anybody explain why this is? post.mode = postmodes.signature; SELECT everything FROM everywhere WHERE something = something_else; > 1 Row Returned > 42

        R Offline
        R Offline
        Ravindra Sadaphule
        wrote on last edited by
        #3

        This behavious is correct. Static variable has lifetime of global variable but it has a scope of local variable. Hence the value of static variable persists across multiple calls to the function. Unlike nonstatic members where each instance of object has its own copy of variables, the static variable is shared across all instances i.e. only one copy od static variable is available across all instances of objects. Ravindra Sadaphule MCSD.NET

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups