C
C#2w ago
TahaA

✅ where constraint on interface method

Is it even possible? Like public void RefuelCar<c>(c Car) where c : IUseGas.Refuel Here the generic type is constrainted to where it implements an interface's method, whereas the name of the interface is IUseGas
16 Replies
TahaA
TahaA2w ago
The name of the method is Refuel() ofc
Angius
Angius2w ago
That doesn't make sense And doesn't work, of course Classes have to implement all methods of an interface
TahaA
TahaA2w ago
That's what I thought. I asked because I just came back from my endterm exam where they demanded that we used it like that.
Angius
Angius2w ago
It makes no sense to check if a class implements one particular method of it It has to
MODiX
MODiX2w ago
Angius
REPL Result: Failure
interface IFoo
{
void Bar();
}

void Baz<T>(T thing) where T : IFoo.Bar { }
interface IFoo
{
void Bar();
}

void Baz<T>(T thing) where T : IFoo.Bar { }
Exception: CompilationErrorException
- The type name 'Bar' does not exist in the type 'IFoo'
- The type name 'Bar' does not exist in the type 'IFoo'
Compile: 233.972ms | Execution: 0.000ms | React with ❌ to remove this embed.
Angius
Angius2w ago
It won't even compile
TahaA
TahaA2w ago
That's what I thought, too. That's why I didn't do it on the endterm, took my 1.3 points out of 2, so I missed 0.7 because of that. Seems like some teacher made the exam without either checking it himself or let another check. Too condfident in their code and not doing their job right.
Angius
Angius2w ago
Yeah Whoever required you to do this way was high on fentanyl Unless, idk, it wasn't actually C#? Lowercase types and uppercase method params, assuming that's how it looked on the exam, are not a C# naming convention
TahaA
TahaA2w ago
Yeah I was also confused because of how c wasn't uppercase.
Shazanman
Shazanman2w ago
thing is it not also java
TahaA
TahaA2w ago
Nvm I read the instructions wrong
Shazanman
Shazanman2w ago
either its some wacky psuedocode
TahaA
TahaA2w ago
How do I close this thread?
Shazanman
Shazanman2w ago
you cant
Angius
Angius2w ago
$close
MODiX
MODiX2w ago
If you have no further questions, please use /close to mark the forum thread as answered