@kironroy/

instantiated_classes2

C#

instantiated classes Zelda example

fork
loading
Files
  • main.cs
  • CharacterModel.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
using System;
using System.Collections.Generic;

class MainClass {
  public static void Main () 
  {
    
    // *** Class instances almost exclusively store data ***


    // List is a blueprint, instantiating (creating) a list is a type -> just like bool, double, string are types. Character model is also a type.  IT CAN BE USED IN A LIST
    
    List<CharacterModel> characters = new List<CharacterModel>();

    // Variable holds the street (memory) address, NOT the object 
    CharacterModel character = new CharacterModel();  // init 
    // character variable can be reused
    
    character = new CharacterModel();
    character.FirstName = "Zelda";
    character.Role = "Princess";
    characters.Add(character);

    // building new object in a different location but still calling it character 
    // The old address is already built and stays there
    // The new variable (with the same name as character) holds a new address and is for a  brand new "car/house" object
    
    character = new CharacterModel();
    character.FirstName = "Link";
    character.Role = "Hero";
    characters.Add(character);

    character = new CharacterModel();
    character.FirstName = "Gannon";
    character.Role = "Enenmy";
    characters.Add(character);

    // Just copying street address NOT making duplicates 

    foreach (CharacterModel c in characters)
    {
      Console.WriteLine(); // breakline
      Console.WriteLine($"{c.FirstName} is the {c.Role}" );
    }


  }
}