Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6847521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:45:17+00:00 2026-05-27T00:45:17+00:00

Is there a way to have a static class have static data that does

  • 0

Is there a way to have a static class have static data that does not clear itself at the end of the function call?
i.e. given:

static class Class1
{
    static int[] _array;
    static Class1()
    {
         _array = new[] {2};
    }
    public static void FillArray()
    {
        List<int> temp = new List<int>();
        for(int i=0;i<100;i++)
            temp.Add(i);
        _array = temp.ToArray();
    }
    public static int[] GetArray()
    {
        return _array;
    }
}

How can I get GetArray() to return something other than null?

EDIT: I want to call this code:

        int[] array1 = Class1.GetArray();
        for (int i = 0; i < array1.Length;i++ )
            Console.WriteLine(array1[i]);
        Class1.FillArray();
        for (int i = 0; i < array1.Length; i++)
            Console.WriteLine(array1[i]);

and not get two 2s. How can I make that happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:45:18+00:00Added an answer on May 27, 2026 at 12:45 am
            int[] array1 = Class1.GetArray();
            for (int i = 0; i < array1.Length;i++ )
                Console.WriteLine(array1[i]);
            Class1.FillArray();
            for (int i = 0; i < array1.Length; i++)
                Console.WriteLine(array1[i]);
    

    In this code, you are getting the memory address of the first int[] {2} array and storing that as array1. Then when you call FillArray() you are creating the new List of arrays, and only setting its memory back to the _array in the class, not array1. That is not a reference to the memory in the class, but to the actual original array. So then, when you loop back through, you are still looking at the same memory block.

    You should probably be doing this instead:

        int[] array1 = Class1.GetArray();
        for (int i = 0; i < array1.Length;i++ )
            Console.WriteLine(array1[i]);
        Class1.FillArray();
        array1 = Class1.GetArray();
        for (int i = 0; i < array1.Length; i++)
            Console.WriteLine(array1[i]);
    

    Update
    if you change your Class1 to look like this, you will see that you are changing the data in the class.

       static class Class1
       {
          static int[] _array;
          static Class1()
          {
             _array = new[] { 2 };
          }
    
          public static void FillArray()
          {
             List<int> temp = new List<int>();
             for (int i = 0; i < 100; i++)
             {
                temp.Add(i);
    
             }
             _array = temp.ToArray();
             PrintArray();
          }
    
          public static int[] GetArray()
          {
             PrintArray();
             return _array;
          }
    
          private static void PrintArray()
          {
             foreach (int i in _array)
             {
                System.Console.Write(String.Format("{0},", i));
             }
          }
    
       }
    

    Because it will print out the elements in the array after each call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have a mutable static variable in F# class that
My class loader resolves resources to a source that does not have a url
have a Grails domain object that has a custom static function to grab data
Is there a way to force classes in Java to have public static final
Is there any way to have something that looks just like a file on
Is there a way to have a file that is modified / touched whenever
Is there a way to have a RewriteRule fire on any domain that is
I have written an ASP.NET HttpModule and I have a static helper class that
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
I have an piece of code that does calculations on assets. There are many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.