repl.it
@kironroy/

reference_value_type

C#

No description

fork
loading
Files
  • main.cs
  • main.exe
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;

class MainClass {
  public class Person
  {
    public int Age;
  }
 
  public static void Main (string[] args)
   {
  
      // Ints and other primitive types are value type

      Console.WriteLine("___INTS AND OTHER PRIMITIVE TYPES ARE Value TYPES____");
      Breakline();
      
      var number = 1;
      Console.Write("the value of number after the increment method: ");
      Increment(number);

      Breakline();

      Console.WriteLine($"But the value of {number} is unchanged in the Main Class.\n" +
      "number variables value is copied and it DOES NOT effect the orignal value");

     Breakline(); 
     Breakline();

    Console.WriteLine("____CLASSES ARE Reference TYPES____");
    Breakline();
      
      // Classes are Reference type
      
      var person1 = new Person() { Age = 20};
        Console.WriteLine($"Before MakeYoung method the age is: {person1.Age}");
        Console.WriteLine();
      MakeYoung(person1);

      Console.WriteLine("Person class is an example of a refrence type person1 variable.\n" + 
      "Both person1 and the parameter person are referencing \nthe same memory address in the the HEAP."
      );
      
     Breakline();
      
      Console.WriteLine( $"Person was 20 and with the MakeYoung method, the person is: {person1.Age} (2 years younger)");

    

     Breakline();

      Console.WriteLine("Array example:");
      Breakline();

      var array01 = new int[4] {1, 2, 3, 4};
      var array02 = array01;
      array02[0] = 56;

      Console.WriteLine(String.Format("Both array01 and array02 are refrencing each other so they will have the same value array01[0]: {0}, array02[0]: {1}", array01[0], array02[0]));
   }
   
   public static void Increment(int number)
   {
      number += 1;
      Console.WriteLine(number);
      
      
   }

   public static void MakeYoung(Person person)
   {
      person.Age -= 2;
   }

   public static void Breakline()
   {
     Console.WriteLine();
   }

}