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 3432584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:28:02+00:00 2026-05-18T07:28:02+00:00

The exception I get is: The type initializer for MyNamespace.Program threw an exception The

  • 0

The exception I get is:

The type initializer for MyNamespace.Program threw an exception

The inner exception says

Message: Value cannot be null. Parameter name: collection
Source: … HashSet …

This leads me to believe the error is occuring on the line indicated below…

class Program
{
    public static IEnumerable<char> WordCharacters = ExpandCharacterSet("A-Za-z0-9_");
    public static IEnumerable<char> NonWordCharacters = ExpandCharacterSet("^A-Za-z0-9_");
    public static IEnumerable<char> SpaceCharacters = ExpandCharacterSet(" \f\n\r\t\v");
    public static IEnumerable<char> NonSpaceCharacters = ExpandCharacterSet("^ \f\n\r\t\v");
    public static IEnumerable<char> DigitCharacters = ExpandCharacterSet("0-9");
    public static IEnumerable<char> NonDigitCharacters = ExpandCharacterSet("^0-9");
    public static IEnumerable<char> WildCharacters = ExpandCharacterSet("^\n");
    public static IEnumerable<char> AllCharacters = Enumerable.Range(0, 256).Select(Convert.ToChar).Where(c => !char.IsControl(c));

    public static IEnumerable<char> ExpandCharacterSet(string set)
    {
        if (set.Length == 0)
            return "";

        var sb = new StringBuilder();
        int start = 0;
        bool invertSet = false;

        if (set[0] == '[' && set[set.Length - 1] == ']')
            set = set.Substring(1, set.Length - 2);
        if (set[0] == '^')
        {
            invertSet = true;
            set = set.Substring(1);
        }
        set = Regex.Unescape(set);

        foreach (Match m in Regex.Matches(set, ".-.|."))
        {
            if (m.Value.Length == 1)
                sb.Append(m.Value);
            else
            {
                if (m.Value[0] > m.Value[2]) throw new ArgumentException("Invalid character set.");
                for (char c = m.Value[0]; c <= m.Value[2]; ++c)
                    sb.Append(c);
            }
        }

        if (!invertSet) return sb.ToString();

        var A = new HashSet<char>(AllCharacters); // <---- change this to "ABC" and the error goes away
        var B = new HashSet<char>(sb.ToString());
        A.ExceptWith(B);
        return A;
    }

    static void Main(string[] args)
    {
    }
}

But I don’t know why. When I print the chars,

Console.WriteLine(string.Concat(AllChars));

It prints every character as expected. So why does HashSet think it’s null?

  • 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-18T07:28:03+00:00Added an answer on May 18, 2026 at 7:28 am

    I’m guessing that ExpandCharacterSet is being used to initialize another static field. There’s no guarantee on the order in which two static fields will be initialized, and so presuably, it’s trying to initialize the other field before it initializes AllChars.

    Try moving the assignments into an explicit static constructor, with the correct initialization order.

    e.g if your current code has:

    public static IEnumerable<char> AllChars = Enumerable.Range(0, 256).Select(Convert.ToChar).Where(c => !char.IsControl(c));
    public static IEnumerable<char> Expanded = ExpandCharacterSet(...);
    

    Make it instead:

    public static IEnumerable<char> AllChars;
    public static IEnumerable<char> Expanded;
    static <ClassName> {
        AllChars = Enumerable.Range(0, 256).Select(Convert.ToChar).Where(c => !char.IsControl(c));
        Expanded = ExpandCharacterSet(...);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a singleton and when I call it from my UnitTest I get
I am writing a DLL which is loaded by a third party application (The
I have a C# Console application which references Sybase.Data.AseClient . dev machine has version
Oracle installed on the server is 10.2.0.3 Enterprise Edition 64bit (10g). ODAC is showing
After following the instructions here How To Implement URL rewriting with UrlRewriter.Net? I'm getting
I've got a XamlParseException in some code which is trying to select all the
I use swig 2.0.1 + mono 2.6/2.8 on Mac OS X 10.6.4. The overall
I have an application that generates some pretty basic reports. I have installed the
Hope someone helps me with this If CallbackHandler.proxy is static, then everything works fine:

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.