Advertisement
kirilstanoev

Untitled

Aug 2nd, 2021
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.Text;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         var foo = new Foo();
  11.         Console.WriteLine(foo);
  12.         (foo.Something as IDictionary<string, string>).Add("6", "six");
  13.         Console.WriteLine(foo);
  14.  
  15.     }
  16. }
  17.  
  18. class Foo
  19. {
  20.     private readonly Dictionary<string, string> something;
  21.  
  22.     public Foo()
  23.     {
  24.         this.something = new Dictionary<string, string>();
  25.  
  26.         this.something.Add("1", "one");
  27.         this.something.Add("2", "two");
  28.         this.something.Add("3", "three");
  29.         this.something.Add("4", "four");
  30.         this.something.Add("5", "five");
  31.     }
  32.  
  33.     public IReadOnlyDictionary<string, string> Something
  34.     {
  35.         get
  36.         {
  37.             return this.something;
  38.             //return this.something.ToImmutableDictionary();
  39.         }
  40.     }
  41.  
  42.     public override string ToString()
  43.     {
  44.         var sb = new StringBuilder();
  45.         foreach (var item in this.something)
  46.         {
  47.             sb.AppendLine($"{item.Key}: {item.Value}");
  48.         }
  49.  
  50.         return sb.ToString();
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement