Share via

Declare a persistent string array in C#

Lennart Seiron 0 Reputation points
2026-05-24T14:07:25.6266667+00:00

Declare a persistent string array in c#. The string array to be put in a separate class.

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other

A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.

0 comments No comments

1 answer

Sort by: Most helpful
  1. AI answer

    2026-05-24T14:07:33.8866667+00:00

    Use a static field in a separate class so the array instance lives for the lifetime of the AppDomain.

    public static class AppData
    {
        // Persistent string array, accessible from anywhere in the app
        public static string[] Values = new string[10];
    }
    

    Usage:

    // Read
    string first = AppData.Values[0];
    
    // Write
    AppData.Values[0] = "Hello";
    

    If the array size should not change, keep it as above. If the number of elements may change over time, consider using a List<string> instead of a fixed-size array.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.