#C# List.Add() returns void

51 messages · Page 1 of 1 (latest)

autumn cape
#

I'm currently stuck on the tracks on tracks on tracks exercise, for some reason, using .Add always returns void no matter what, any ideas why?

    {
        return languages.Add(language);
    }```
```TracksOnTracksOnTracks.cs(16,16): error CS0029: Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'```
fickle agate
#

what do you think .Add() method does?

#

Let say i have : lst = [1,2,3,4,5]
then i call lst.Add(6)

#

what gonna happen?

autumn cape
#

hi again!

fickle agate
#

hello

autumn cape
#

I'm assuming it adds (in this case) a string to the list

autumn cape
fickle agate
#

Right. And you want to have those values return or not in this case?

autumn cape
#

I do

fickle agate
#

Then what should you return?

autumn cape
fickle agate
#

Yeah i mean in the context of the function

#

Which variable should be return

#

let me give you another example

#

let say you gimme a doll so i can add something nice to it

#

and i did (this is the add method)

#

then how do you get the doll back with its new stuff added?

autumn cape
#

just, take the doll from your hands

#

the new stuff is added to it already

fickle agate
#

right. so i return the doll to you

#

so you understand what should be returned now?>

autumn cape
#

would that be return languages.Add(language);

fickle agate
#

no. that is the process

#

that is me adding stuff to the doll

#

you want the doll back

autumn cape
#

OH

#

so

#

this isn't doll + accessories for example?

#

but most methods like this aren't the process and give the result instead

#

ahhh I see why it would be that way

fickle agate
#

correct. that is different

#
public void Add (T item);
#

what it does is it modify the list in place

autumn cape
#

I see

#

thank you!

#

I do have a question though

#

is there a more efficient way?

#

maybe something that could be written in a single line

fickle agate
#

I think this is the way to add to a list in C#
I think in python you could do something like : return list + list, but it is not a thing here
and dont be fooled into thinking because it happen in a single line it is more efficient

#

when you do : list + list in python, what happen is you creating a whole new list and return it
which in some language, the preferred way. however a lot of the time you just want to keep working with your current list, so modify it is a better choice

autumn cape
#

thanks for the explanation!

#

I think that's all, I'm going to close the thread

fickle agate
#

no problem. also look into mutable and immutable

autumn cape
#

will do!!

fickle agate
#

i think for you to understand this in a deeper level, you gonna have to wait until you start doing linked list data structure

autumn cape
#

makes sense, there's some things I haven't bothered looking into until I understand more of the basics

fickle agate
#

and another point, methods are basically function

#

but not all function have to return something

autumn cape
#

yeah, you can basically have methods simply to modify various variables etc by marking them as void