Advertisement
zoltanvi

Value type + reference type

Jan 14th, 2023
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ValueTypeVsReferenceType
  9. {
  10.     internal class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             ReferenceType akarmi = new ReferenceType();
  15.  
  16.             int number = 10;
  17.             PrintNumber(akarmi.asd);
  18.  
  19.             ArrayExample array = new ArrayExample();
  20.             array.Initialize();
  21.  
  22.  
  23.             BoxingExample boxingExample = new BoxingExample();
  24.  
  25.             object boxedNumber = boxingExample.BoxNumber(42);
  26.            
  27.         }
  28.  
  29.         private static void PrintNumber(ReferenceType referenceType)
  30.         {
  31.             Console.WriteLine(referenceType);
  32.             referenceType.asd = 30;
  33.         }
  34.  
  35.         public static void PrintNumber(int asd)
  36.         {
  37.             Console.WriteLine(asd);
  38.             asd = 30;
  39.         }
  40.     }
  41.  
  42.     public class ReferenceType
  43.     {
  44.         public static int Vaaaaa;
  45.  
  46.         public static int Vaaaaa2 = GetLofac();
  47.         private static int GetLofac() => 5 + Vaaaaa;
  48.  
  49.         public int asd = GetLofac();
  50.  
  51.         static ReferenceType()
  52.         {
  53.             Vaaaaa = 22;
  54.         }
  55.  
  56.         public int MyProperty => 10;
  57.        
  58.         public int MyProperty2
  59.         {
  60.             get
  61.             {
  62.                 return 10;
  63.             }
  64.         }
  65.  
  66.         public ReferenceType()
  67.         {
  68.             MyProperty6 = "555";
  69.         }
  70.  
  71.         public string MyProperty3 => "valami";
  72.  
  73.         public readonly string MyProperty5 = "valami";
  74.  
  75.         public string MyProperty6 { get; } = Calculate();
  76.  
  77.  
  78.         private static string Calculate()
  79.         {
  80.             return "valami" + "_20";
  81.         }
  82.  
  83.         public string MyProperty4
  84.         {
  85.             get
  86.             {
  87.                 return "valami";
  88.             }
  89.         }
  90.     }
  91.  
  92.     public class ArrayExample
  93.     {
  94.         public void Initialize()
  95.         {
  96.             int[] array = new int[10];
  97.             string valami = "empty";
  98.             ChangeString(valami);
  99.             Console.WriteLine(valami);
  100.         }
  101.  
  102.         public void ChangeString(string str)
  103.         {
  104.             str = "hehehehee";
  105.  
  106.             MyEnum two = MyEnum.None;
  107.             MyEnum three = MyEnum.Three;
  108.  
  109.             int first = (int)two;
  110.             int second = (int)three;
  111.  
  112.             MyEnum flag = MyEnum.One | MyEnum.Two;
  113.  
  114.             int flagInt = 1 | 2;
  115.  
  116.             if ((flag & MyEnum.One) == MyEnum.One)
  117.             {
  118.                 if ((flag & MyEnum.Two) == MyEnum.Two)
  119.                 {
  120.                     string valami2 = null;
  121.                     string valami3 = valami2 ?? "lel";
  122.                     string valami4 = valami2 == null ? "lel" : valami2;
  123.                    
  124.                     Nullable<bool> valami = new Nullable<bool>();
  125.  
  126.                     if (valami.HasValue)
  127.                     {
  128.                         Console.WriteLine(valami.Value);
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.     }
  134.  
  135.  
  136.     public class BoxingExample
  137.     {
  138.         public object BoxNumber(int number)
  139.         {
  140.             return number;
  141.         }
  142.     }
  143.  
  144.     public enum MyEnum
  145.     {
  146.         None = 8,
  147.         One = 1,
  148.         Two = 2,
  149.         Three = 8,
  150.     }
  151.  
  152.     class Example
  153.     {
  154.         static int x = 1;
  155.         int y = 2;
  156.         static Example() { Console.WriteLine("Static constructor"); }
  157.         public Example() { Console.WriteLine("Instance constructor"); }
  158.     }
  159.  
  160.  
  161.  
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement