© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
7 replies
Someone’s Shadow

❔ How to input a reference of a class into a function with an argument of an interface

`
I want to save the result of whatever StartFight does to fighter1 and fighter 2, specifically the health and other things down the line
Hero player = new Hero(20,30,10, "Hero");
Enemy goblin = new Enemy(3,3,2, "Goblin");
BattleField plains = new BattleField();
plains.StartFight(player, goblin);

class BattleField
{
  public void StartFight(IFighter fighter1, IFighter fighter2) {...}
}
class Hero : IFighter {...}
class Enemy : IFighter {...}
Hero player = new Hero(20,30,10, "Hero");
Enemy goblin = new Enemy(3,3,2, "Goblin");
BattleField plains = new BattleField();
plains.StartFight(player, goblin);

class BattleField
{
  public void StartFight(IFighter fighter1, IFighter fighter2) {...}
}
class Hero : IFighter {...}
class Enemy : IFighter {...}

using out or ref throws out an error
Argument 1: cannot convert from 'out Test.Adventure.Hero' to 'out Test.Adventure.IFighter' csharp(CS1503)
Argument 1: cannot convert from 'ref Test.Adventure.Hero' to 'ref Test.Adventure.IFighter' csharp(CS1503)
Argument 1: cannot convert from 'out Test.Adventure.Hero' to 'out Test.Adventure.IFighter' csharp(CS1503)
Argument 1: cannot convert from 'ref Test.Adventure.Hero' to 'ref Test.Adventure.IFighter' csharp(CS1503)

any other solutions than just returning the IFighter arguments with a tuple then assigining values individually outside the method?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Function argument class reference / clone (best practice(s))
C#CC# / help
9mo ago
✅ How to do the C# equivalent of passing an std::function into a class
C#CC# / help
3y ago
✅ How do I reference a specific private instance of a class
C#CC# / help
2y ago
❔ Function with generic argument as a setter
C#CC# / help
3y ago