❔ Im learning refs and outs

i keep getting cs0177 errors if someone could take a look public static void RemoveAllHeroes(string name, out List<Hero> sub) { List<Hero> _heroRem = new List<Hero>();
for (int i = 0; i < _heroes.Count; i++) { if (_heroes[i].Name.StartsWith(name, StringComparison.OrdinalIgnoreCase)) { _heroRem.Add(_heroes[i]); _heroes.RemoveAt(i); } } }
35 Replies
Jimmacle
Jimmacle16mo ago
what does the error tell you? also, $code
MODiX
MODiX16mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Kouhai
Kouhai16mo ago
You need to assign the out parameter
Jimmacle
Jimmacle16mo ago
i was trying to get him to actually read the error message because that's exactly what it says LUL
Kouhai
Kouhai16mo ago
kekkek
BI || CrescentThief
sorry new to all this
Jimmacle
Jimmacle16mo ago
so what is the message telling you to do?
BI || CrescentThief
the out parameter 'sub' must be assigned to
Jimmacle
Jimmacle16mo ago
did you try doing that?
BI || CrescentThief
i thought that involves the new list i created added to that list
Jimmacle
Jimmacle16mo ago
that list has nothing to do with the parameter in your method sub must have a value assigned before the method returns
Kouhai
Kouhai16mo ago
You can think of an out parameter as an empty box that has to be filled before you return
BI || CrescentThief
public static void RemoveAllHeroes(string name, out List<Hero> sub)
{
List<Hero> sub = new List<Hero>();

for (int i = 0; i < _heroes.Count; i++)
{
if (_heroes[i].Name.StartsWith(name, StringComparison.OrdinalIgnoreCase))
{
sub.Add(_heroes[i]);
_heroes.RemoveAt(i);
}
}
}
public static void RemoveAllHeroes(string name, out List<Hero> sub)
{
List<Hero> sub = new List<Hero>();

for (int i = 0; i < _heroes.Count; i++)
{
if (_heroes[i].Name.StartsWith(name, StringComparison.OrdinalIgnoreCase))
{
sub.Add(_heroes[i]);
_heroes.RemoveAt(i);
}
}
}
ok
Jimmacle
Jimmacle16mo ago
sub is already declared, you can't redeclare it like that just sub = new List<Hero>();
BI || CrescentThief
i thought you would need that first part :/
arion
arion16mo ago
I'm curious. Is there a performance hit with the out keyword as an alternative to a Tuple?
Jimmacle
Jimmacle16mo ago
out is basically ref with a guarantee that the method will assign a value you'd have to bench both ways, but here you wouldn't even need a tuple since it currently returns void
arion
arion16mo ago
yea, though in many cases i've seen out used its usually for things that start with the method name "Try" xD
Jimmacle
Jimmacle16mo ago
that convention has existed before we got better tuples
Kouhai
Kouhai16mo ago
Also, the way you're removing elements has a bug, you need to iterate from the end not that start, otherwise you'll miss some items
BI || CrescentThief
from the end of it? so like working backwords? or is it that count--
Kouhai
Kouhai16mo ago
Yeah work backwards for (int i = _heroes.Count - 1; i > 0; i--)
Jimmacle
Jimmacle16mo ago
if you go from start to end, when you remove an item the items after it get shifted back so you'll end up skipping the item after it
BI || CrescentThief
oh thats really good to know never thought about it like that
Jimmacle
Jimmacle16mo ago
also i'm pretty sure you'll hit an indexoutofrangeexception if you ever remove an item, since the count you set up your loop with is no longer accurate
MODiX
MODiX16mo ago
Kouhai#8274
REPL Result: Success
var v = new List<int>() {2, 3, 4};
for(int i = 0; i < v.Count; i++)
{
if(v[i] == 3 || v[i] == 4)
{
v.RemoveAt(i);
}
}
v
var v = new List<int>() {2, 3, 4};
for(int i = 0; i < v.Count; i++)
{
if(v[i] == 3 || v[i] == 4)
{
v.RemoveAt(i);
}
}
v
Result: List<int>
[
2,
4
]
[
2,
4
]
Compile: 622.418ms | Execution: 49.956ms | React with ❌ to remove this embed.
BI || CrescentThief
Kouhai how are you able to show it like that
Kouhai
Kouhai16mo ago
$eval
MODiX
MODiX16mo ago
To compile C# code in Discord, use !eval Example:
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!
BI || CrescentThief
ok sweet thanks
Buddy
Buddy16mo ago
$refvsvalue
Buddy
Buddy16mo ago
ref ⬆️
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.