none
Auto generated source file and static files

    Question

  • I need to auto generate a cs file that will be compiled with other code as a library. The public field is a very large read only dictionary whose underlying dictionary is case insensitive. Whats the right way to arrange the private and public fields?
    public static class Foo
    {
       private static readonly Dictionary<string, string> data =
            new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
            {
                { "a", "Interval" },
                { "b", "String(Unicode)" },
                { "c", "Interval" }
                // ...
            };
    
        public static ReadOnlyDictionary<string, string> Data => new ReadOnlyDictionary<string, string>(data);
    }
    Does the return type of the public field get instantiated only once, or every access?
    Thursday, April 27, 2017 1:19 AM

Answers

  • I disagree with cheong00. The way the code sample is written, the property is static but the property instantiates a new ReadOnlyDictionary every time it is called. A better idea might be to use a property with a getter that instantiates just once.

       class Program
       {
          static void Main(string[] args)
          {
             ReadOnlyDictionary<string, string> data1 = Foo.DataA;
             ReadOnlyDictionary< string, string> data2 = Foo.DataA;
    
             if (data1 == data2)
                Console.WriteLine("Equal DataA");
             else
                Console.WriteLine("Not Equal DataA");
    
    
             data1 = Foo.DataB;
             data2 = Foo.DataB;
    
             if (data1 == data2)
                Console.WriteLine("Equal DataB");
             else
                Console.WriteLine("Not Equal DataB");
          }
    
       }
    
       public static class Foo
       {
          private static readonly Dictionary<string, string> data =
               new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
               {
                { "a", "Interval" },
                { "b", "String(Unicode)" },
                { "c", "Interval" }
                  // ...
               };
    
          // This property instantiates every time.
          public static ReadOnlyDictionary<string, string> DataA => new ReadOnlyDictionary<string, string>(data);
    
          // This property instantiates just once.
          public static ReadOnlyDictionary<string, string> DataB { get; } =  new ReadOnlyDictionary<string, string>(data);
       }

    • Marked as answer by Ritmo2k Thursday, April 27, 2017 2:55 AM
    Thursday, April 27, 2017 2:19 AM

All replies

  • Static member of the class only get instantiated once so no need to worry. And the external application will be accessing this member by reference (so it will not be copied. And if it's not ReadOnlyDictionary and just Dictionary, all changes one instance of class made to the content will be visible to other instance using it).

    Btw, if you're that worry about it, you can also create static method like GetValue() that will accept a string and return the corresponding string. (Actually that's what we usually do before the existence of ReadOnlyDictionary class).


    Thursday, April 27, 2017 1:38 AM
    Answerer
  • I disagree with cheong00. The way the code sample is written, the property is static but the property instantiates a new ReadOnlyDictionary every time it is called. A better idea might be to use a property with a getter that instantiates just once.

       class Program
       {
          static void Main(string[] args)
          {
             ReadOnlyDictionary<string, string> data1 = Foo.DataA;
             ReadOnlyDictionary< string, string> data2 = Foo.DataA;
    
             if (data1 == data2)
                Console.WriteLine("Equal DataA");
             else
                Console.WriteLine("Not Equal DataA");
    
    
             data1 = Foo.DataB;
             data2 = Foo.DataB;
    
             if (data1 == data2)
                Console.WriteLine("Equal DataB");
             else
                Console.WriteLine("Not Equal DataB");
          }
    
       }
    
       public static class Foo
       {
          private static readonly Dictionary<string, string> data =
               new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
               {
                { "a", "Interval" },
                { "b", "String(Unicode)" },
                { "c", "Interval" }
                  // ...
               };
    
          // This property instantiates every time.
          public static ReadOnlyDictionary<string, string> DataA => new ReadOnlyDictionary<string, string>(data);
    
          // This property instantiates just once.
          public static ReadOnlyDictionary<string, string> DataB { get; } =  new ReadOnlyDictionary<string, string>(data);
       }

    • Marked as answer by Ritmo2k Thursday, April 27, 2017 2:55 AM
    Thursday, April 27, 2017 2:19 AM
  • Oh, I think the => is just a typo there.
    Thursday, April 27, 2017 2:42 AM
    Answerer
  • Nice, I forgot about the getter syntax in Ante's example. The => in mine is the new expression body syntax, its certainly valid:)
    • Edited by Ritmo2k Thursday, April 27, 2017 2:56 AM
    Thursday, April 27, 2017 2:56 AM