constk

Some C# funny ctor pattern

Mar 4th, 2021
900
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 Car
  7.     {
  8.         private string carColor;
  9.         private string carModel;
  10.         private string carId;
  11.  
  12.         public Car() : this("", "", "") => Console.WriteLine("1 ctor call");
  13.         public Car(string color) : this(color, "", "") => Console.WriteLine("2 ctor call");
  14.         public Car(string color, string model) : this(color, model, "") => Console.WriteLine("3 ctor call");
  15.  
  16.         public Car(string color, string model, string id)
  17.         {
  18.             carColor = color;
  19.             carModel = model;
  20.             carId = id;
  21.             Console.WriteLine("4 ctor call");
  22.         }
  23.     }
  24.  
  25.     class Bibika
  26.     {
  27.         private string bibikaColor;
  28.         private string bibikaModel;
  29.         private string bibikaId;
  30.  
  31.         public Bibika(string color = "", string model = "", string id = "")
  32.         {
  33.             bibikaColor = color;
  34.             bibikaModel = model;
  35.             bibikaId = id;
  36.             Console.WriteLine("ctor call");
  37.         }
  38.     }
  39.  
  40.     class Program
  41.     {
  42.         static void Main(string[] args)
  43.         {
  44.             CultureInfo.CurrentCulture = new CultureInfo("en-US", false);
  45.  
  46.             Car car1 = new Car();
  47.             Car car2 = new Car("green");
  48.             Car car3 = new Car("blue", "lada vesta");
  49.             Car car4 = new Car("red", "camara", "103456");
  50.  
  51.             Bibika bibika1 = new Bibika();
  52.             Bibika bibika2 = new Bibika(color: "green");
  53.             Bibika bibika3 = new Bibika(color: "blue", model: "lada vesta");
  54.             Bibika bibika4 = new Bibika(color: "red", id: "103456");
  55.             Bibika bibika5 = new Bibika(model: "ferrari", id: "111111");
  56.             Bibika bibika6 = new Bibika("pink", "vas", "222222");
  57.  
  58.             Console.ReadKey();
  59.         }
  60.     }
  61. }
  62.  
RAW Paste Data