constk

Some C# funny read only properties testing

Mar 5th, 2021
692
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Globalization;
  3.  
  4. namespace CSharpTest
  5. {
  6.     class PropertiesTesting
  7.     {
  8.         private int somePrivateVar;
  9.  
  10.         public string SomePrivateThing { get; } // read only property
  11.         public int SomePrivateVar { get; } //
  12.  
  13.  
  14.         public PropertiesTesting(string var) => SomePrivateThing = var; // ctor can change it
  15.         public PropertiesTesting(int var) => SomePrivateVar = var; // ctor can change this one too
  16.  
  17.         //public void ChangeSomEPrivateThing() => SomePrivateThing = "gg"; // but method can't
  18.         //public void ChangeSomePrivateVar() => SomePrivateVar = 1; // also can't
  19.  
  20.         public void ChangeSomePrivateVar() => somePrivateVar = 1; // but we can do this!
  21.         /*
  22.          but there is the problem: in this case we should manually specify that value will be linked with property
  23.          or there will be two different entities, so in this case
  24.          "somePrivateVar"(field) and "SomePrivateVar"(property) aren't same object
  25.  
  26.          we will show how it should be done for var value and Var property in Entity class
  27.              */
  28.  
  29.         public void showData()
  30.         {
  31.             Console.WriteLine($"SomePrivateThing = {SomePrivateThing}");
  32.             Console.WriteLine($"SomePrivateVar = {SomePrivateVar}");
  33.             Console.WriteLine($"somePrivateVar = {somePrivateVar}");
  34.         }
  35.     }
  36.  
  37.     class Entity
  38.     {
  39.         private int var; // field
  40.         public int Var => var; // read only property
  41.  
  42.         //public Entity(int value) => Var = value; here ctor already can't initialize read only property
  43.         public Entity(int value) => var = value; // but it can initialize field
  44.  
  45.         // same to methods
  46.         //public void Method() => Var = 100; there is no way!
  47.         public void Method() => var = 100; // here it is!
  48.  
  49.         public void showData() => Console.WriteLine($"var = {var}\nVar = {Var}");
  50.     }
  51.  
  52.     class Program
  53.     {
  54.         static void Main(string[] args)
  55.         {
  56.             CultureInfo.CurrentCulture = new CultureInfo("en-US", false);
  57.  
  58.             PropertiesTesting pt = new PropertiesTesting("auf");
  59.             // pt.SomePrivateThing = "awooooo!"; can't do such stuff - compile error
  60.  
  61.             PropertiesTesting pt2 = new PropertiesTesting(100);
  62.             pt2.ChangeSomePrivateVar();
  63.             pt2.showData(); // "", 100 and 1
  64.  
  65.             Entity e = new Entity(5);
  66.             e.showData(); // 5 and 5
  67.             e.Method();
  68.             e.showData(); // 100 and 100 as it should be
  69.  
  70.             Console.ReadKey();
  71.         }
  72.     }
  73. }
  74.  
RAW Paste Data