C
C#8mo ago
LastExceed

✅ how can i let an interface implement a function of a super-interface, instead of shadowing it?

public class MyClass : IOne;

public interface IOne : ITwo
{
public void Foo()
{
Console.WriteLine();
}
}

public interface ITwo
{
public void Foo();
}
public class MyClass : IOne;

public interface IOne : ITwo
{
public void Foo()
{
Console.WriteLine();
}
}

public interface ITwo
{
public void Foo();
}
this doesnt compile, because IOne.Foo is considered a separate function that shadows ITwo.Foo. how do i tell IOne to provide a default implementation for ITwo.Foo instead of shadowing it?
8 Replies
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX8mo ago
TeBeCo
REPL Result: Success
using System;

var foo = new MyClass();

foo.Foo();
((IOne)foo).Foo();
((ITwo)foo).Foo();

public interface IOne : ITwo {}

public interface ITwo
{
void Foo(); // no scope on interface member ... because it's the interface
}

public class MyClass : IOne
{
public void Foo()
{
Console.WriteLine("1");
}

void ITwo.Foo()
{
Console.WriteLine("2");
}
}
using System;

var foo = new MyClass();

foo.Foo();
((IOne)foo).Foo();
((ITwo)foo).Foo();

public interface IOne : ITwo {}

public interface ITwo
{
void Foo(); // no scope on interface member ... because it's the interface
}

public class MyClass : IOne
{
public void Foo()
{
Console.WriteLine("1");
}

void ITwo.Foo()
{
Console.WriteLine("2");
}
}
Console Output
1
2
2
1
2
2
Compile: 504.000ms | Execution: 28.647ms | React with ❌ to remove this embed.
LastExceed
LastExceedOP8mo ago
this is exactly what i was looking for, thanks!
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
LastExceed
LastExceedOP8mo ago
i dont know what it means either xD
Unknown User
Unknown User8mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX8mo ago
TeBeCo
REPL Result: Success
using System;

var foo = new MyClass();

((IOne)foo).Foo();
((ITwo)foo).Foo();

foo.Foo();

public interface IOne
{
void Foo(); // no scope on interface member ... because it's the interface
}

public interface ITwo
{
void Foo(); // no scope on interface member ... because it's the interface
}

public class MyClass : IOne, ITwo
{
void IOne.Foo() => Console.WriteLine("1");

void ITwo.Foo() => Console.WriteLine("2");

public void Foo() => Console.WriteLine("other");
}
using System;

var foo = new MyClass();

((IOne)foo).Foo();
((ITwo)foo).Foo();

foo.Foo();

public interface IOne
{
void Foo(); // no scope on interface member ... because it's the interface
}

public interface ITwo
{
void Foo(); // no scope on interface member ... because it's the interface
}

public class MyClass : IOne, ITwo
{
void IOne.Foo() => Console.WriteLine("1");

void ITwo.Foo() => Console.WriteLine("2");

public void Foo() => Console.WriteLine("other");
}
Console Output
1
2
other
1
2
other
Compile: 508.964ms | Execution: 29.475ms | React with ❌ to remove this embed.
LastExceed
LastExceedOP8mo ago
well its an isolated example. my use case isn't that simple of course
Want results from more Discord servers?
Add your server