✅ How to add data to a list that is outside of Main()?
I know there are some errors but I can't figure out how to solve this. Also when calling the insertData the list will be resetted every new call?
13 Replies
yes cause you declare it in the method
insertData
, So list
is not accessible in main
you must instantiate your list
variable externally OR pass it directly into the method and return it / using referenceRubyNova#0404
if you're unsure of by ref. vs. by val. look at this gif until it makes sense:
https://media.giphy.com/media/xUPGcLrX5NQgooYcG4/giphy.gif
Embed Type
Gifv
Quoted by
<@!358653201513447465> from #chat (click here)
React with ❌ to remove this embed.
geez I dont even know what the word instantiate means. I dont even know where to begin
declaration :
List<KeyValuePair<string, int>> list;
instantiation :
list = new List<KeyValuePair<string, int>>();
Declaring a variable means to introduce a new variable to the program. You define its type and its name. Instantiate - Instantiating a class means to create a new instance of the class.
Stack Overflow
Meanings of declaring, instantiating, initializing and assigning an...
Technically what are the meanings and differences of the terms declaring, instantiating, initializing and assigning an object in C#?
I think I know the meaning of assigning but I have no formal
thanks, appreciated. now I follow 🙂
np
Am I even doing something right here or?
you need to add
using System;
to call Console.WriteLine()
otherwise the code seems correct
i guess you are learning to code, but if you put everything in the Program.cs
, it is a good practice to put the main
method on topyeah Im a beginner. Ive worked a little bit with python and php before
and separate the class Animal (without "s") in an other file
alright, thanks alot man
I will close this if there is nothing more to add
yep