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 field initialization

static field initialization

Scheduled Pinned Locked Moved C#
6 Posts 4 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.
  • H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #1

    class MyClass3
    {
    public static int i;

    }
    class Program
    {
        static void Main(string\[\] args)
        {
           
           
            Console.WriteLine(MyClass3.i);
            
            Console.ReadKey();
        }
    }
    

    I get a zero on the console. How is the static field getting initialized.

    X G L 3 Replies Last reply
    0
    • H humayunlalzad

      class MyClass3
      {
      public static int i;

      }
      class Program
      {
          static void Main(string\[\] args)
          {
             
             
              Console.WriteLine(MyClass3.i);
              
              Console.ReadKey();
          }
      }
      

      I get a zero on the console. How is the static field getting initialized.

      X Offline
      X Offline
      Xmen Real
      wrote on last edited by
      #2

      simple

      MyClass3.i = XX

      XX any numeric value

      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y<p?jxsg-b$f4ia> -------------------------------------------------------- 128 bit encrypted signature, crack if you can

      1 Reply Last reply
      0
      • H humayunlalzad

        class MyClass3
        {
        public static int i;

        }
        class Program
        {
            static void Main(string\[\] args)
            {
               
               
                Console.WriteLine(MyClass3.i);
                
                Console.ReadKey();
            }
        }
        

        I get a zero on the console. How is the static field getting initialized.

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        You can have a static constructor to initialize it or you can just assign it any value you wish from the code that uses this static class.

        Giorgi Dalakishvili #region signature my articles #endregion

        1 Reply Last reply
        0
        • H humayunlalzad

          class MyClass3
          {
          public static int i;

          }
          class Program
          {
              static void Main(string\[\] args)
              {
                 
                 
                  Console.WriteLine(MyClass3.i);
                  
                  Console.ReadKey();
              }
          }
          

          I get a zero on the console. How is the static field getting initialized.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          If your question was, why the value is 0 although it has nowhere been initialized, then the C# language specification[^] tells you why: 5.2 Default values The following categories of variables are automatically initialized to their default values: * Static variables. * Instance variables of class instances. * Array elements. The default value of a variable depends on the type of the variable and is determined as follows: * For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor (§4.1.2). * For a variable of a reference-type, the default value is null. Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bits-zero to represent the null reference. regards

          H 1 Reply Last reply
          0
          • L Lost User

            If your question was, why the value is 0 although it has nowhere been initialized, then the C# language specification[^] tells you why: 5.2 Default values The following categories of variables are automatically initialized to their default values: * Static variables. * Instance variables of class instances. * Array elements. The default value of a variable depends on the type of the variable and is determined as follows: * For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor (§4.1.2). * For a variable of a reference-type, the default value is null. Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bits-zero to represent the null reference. regards

            H Offline
            H Offline
            humayunlalzad
            wrote on last edited by
            #5

            Instance variables are initialized when we make an object of the class with the "new" keyword. Similarly with arrays. But in this case we dont use the new keyword. So how does it get initialized to its default value.

            L 1 Reply Last reply
            0
            • H humayunlalzad

              Instance variables are initialized when we make an object of the class with the "new" keyword. Similarly with arrays. But in this case we dont use the new keyword. So how does it get initialized to its default value.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              int is a value type, so it's default (empty) constructor will be called automatically by the runtime by the time you try to access it. regards

              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