Static constructor 2 : Static constructor : Class C# Examples


C# Examples » Class » Static constructor »

 

Static constructor 2









    
using  System;

class  MyClass
{
      private  static  Random  RandomKey;

      static  MyClass()                                          
      {
            RandomKey  =  new  Random();                                      
      }

      public  int  GetValue()
      {
            return  RandomKey.Next();
      }
}

class  Program
{
      static  void  Main()
      {
            MyClass  a  =  new  MyClass();
            MyClass  b  =  new  MyClass();
            Console.WriteLine("Next  Random  #:  {0}",  a.GetValue());
            Console.WriteLine("Next  Random  #:  {0}",  b.GetValue());
      }
}
    
   
  
   



Output

Next Random #: 1768997546
Next Random #: 1565321362


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Class
» Static constructor