-4

public List<ItemData> itemBase; When i declare this list i can use it without definition is there benefit in defining while declaring variable?

public List<ItemData> itemBase = new List<ItemData>();

I'd be very grateful if someone could explain.

EDIT:

public List<ItemData> itemBase;
    
    
        ItemData i = new ItemData();
        i.itemName = "dsadsad";
        i.itemSprite = "lolo";
        ItemData i2 = new ItemData();
        i2.itemName = "dsadsad2";
        i2.itemSprite = "lolo2";
        itemBase.Add(i);
        itemBase.Add(i2);

You guys said i can't use .add function but i can use. Yes i define at ItemData i2 but i didn't instantiate list i just instantiate list member.

Bagbaq
  • 1
  • 3
  • 4
    *i can use it without definition* - Did you try `itemBase.Add(...) `in the first case? – jps Jul 26 '23 at 11:39
  • "When i declare this list i can use it without definition" - what do you mean by "definition"? It's more *initialization* that's missing here - so the field has its default value of null. You simply haven't assigned an initial value to it. – Jon Skeet Jul 26 '23 at 11:44
  • related: https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration @JonSkeet I understand the OP, I learned the same terminology long time ago. – jps Jul 26 '23 at 11:52
  • 3
    @jps: That may well be the term used in other languages, but it's not used like that in C#. I do understand that using the right terminology for the language is difficult when you're new to a language - but I'd argue that's one of the things that's important to try to do from the start. – Jon Skeet Jul 26 '23 at 11:57
  • Your posted code doesn't work. https://dotnetfiddle.net/MS7xAy. The error message is "Use of unassigned local variable 'itemBase'", exactly as I would expect. – Robert Harvey Jul 26 '23 at 12:14
  • @RobertHarvey Idk this works both way on unity. I think i should init the variable or there could be issues in future. – Bagbaq Jul 26 '23 at 12:28
  • Unity is a different deal. Initialization takes place in other ways. I see that your whole story is gradually beginning to emerge. – Robert Harvey Jul 26 '23 at 12:32
  • Note that there are many different ways to initialize variables. Object Initializers, Dependency Injection Containers, Deserialization, Reflection. They all amount to assigning an initial value to the variable, and this is taking place in Unity somewhere else. – Robert Harvey Jul 26 '23 at 12:36
  • @RobertHarvey: That fiddle doesn't match the code in the question, which is clearly a field due to the `public` modifier. (It's unfortunate that the OP hasn't provided a complete example, but in all three declarations they've included `public`.) – Jon Skeet Jul 26 '23 at 13:10
  • @JonSkeet: That was before I found out the OP was using Unity, and I had to make some assumptions, since the OP provided incomplete code. – Robert Harvey Jul 26 '23 at 13:21
  • @RobertHarvey: Given that `public` was *everywhere* (and that they've said they can use it), I think it's reasonable to assume it's a field. Unity is irrelevant to this IMO. – Jon Skeet Jul 26 '23 at 13:23
  • @JonSkeet: { shrug } Their original code won't work with a field either. Except in Unity, of course, which has a different mechanism for initializing variables. – Robert Harvey Jul 26 '23 at 13:24
  • I'm confused as to what you mean. `public List itemBase;` is fine in regular C# - you can definitely use the field (read from it, write to it) without initializing it at the point of declaration. It will have an initial value of `null`, sure - but that's not the same as not being able to use the field. The code added in the revision 3 would throw a NullReferenceException in "regular" .NET, agreed - but I see no reason to jump from that to "that means they must have been talking about a local variable". – Jon Skeet Jul 26 '23 at 13:38

1 Answers1

3
public List<ItemData> itemBase;

declares a variable of type List<ItemData>. Because List<ItemData> is a reference type, its initial value is null. This is perfectly valid C# code, but if you try to execute a method on it, such as

itemBase.Add(myItemData);

you will get a null reference exception, because the variable isn't referencing any object on which you can execute the Add method.

When you then write

itemBase = new List<ItemData>();

You instantiate an object of type List<ItemData> and assign it to itemBase, and then your call to the Add method will work.

In C#, the initial (first) assignment of a value to a variable is called initialization.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 1
    It might be worth mentioning "Target typed new", i.e `List itemBase = new ();` – JonasH Jul 26 '23 at 12:05
  • @jonasH Or `var itemBase = new List();`. – Robert Harvey Jul 26 '23 at 12:07
  • @JonasH We should try not to confuse freshmans with the shortcuts of a language before having a solid base in a language. – Ralf Jul 26 '23 at 12:10
  • Yes, but for fields/properties you need to use target typed new if you do not want to repeat the type. Granted, it is not entirely clear if the OP is asking about fields or local variables. – JonasH Jul 26 '23 at 12:12