Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Construct a list of classes before treatment

Discussion in 'Programming Questions and Suggestions' started by BobbySix, Mar 11, 2017.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. BobbySix Trainee Engineer

    Messages:
    32
    Hi everyone,

    I try to create a list of classes during the data declaration as i could do with a list of structures like this :

    Code:
    public struct t_Item {
    public string Name;
    public int Amount;
    public int Target;
    }
    
    List<t_Item> _IngotItem = new List<t_Item>(){new t_Item{ Name = "Ingot"}, new t_Item{ Name = "Stone"},
    new t_Item{ Name = "Iron"}, new t_Item{ Name = "Nickel"}, new t_Item{ Name = "Silicon"},
    new t_Item{ Name = "Cobalt"}, new t_Item{ Name = "Magnesium"}, new t_Item{ Name = "Silver"},
    new t_Item{ Name = "Gold"}, new t_Item{ Name = "Platinum"}, new t_Item{ Name = "Uranium"}}; 
    I would do the same but substitute struct by a class, and the only solution I've found is to create the list by using class constructor during the treatment.

    Do you know a solution to construct my list of classes before treatment ?

    And anyway, do you know if i can expect a quicker data treatment in this way ?
     
  2. Malware Master Engineer

    Messages:
    9,446
    I don't understand what you're asking. Classes and structs can be made in the same way. And I can't answer whether you "can expect a quicker data treatment", because there's not enough information to determine that. :)

    The difference is that classes is allocated on the heap, and the structs are allocated on the stack. This means that struct allocation is faster, and they don't affect the garbage collector the way heap allocation does. Structs have behavioral differences, and they're highly recommended to always be made immutable to avoid mistakes.
     
    Last edited: Mar 11, 2017
  3. BobbySix Trainee Engineer

    Messages:
    32
    Ok.
    You're right, classes and struct can be made in the same way, I just missed to replace a } by ), for my defense it could be very difficult to differenciate them with the in-game editor ^^'.

    But as I believe understanding, struct might be a more efficient option for static database and classe has more complicated data access but avoid problems with dynamic data construction/destruction ?
     
Thread Status:
This last post in this thread was made more than 31 days old.