Is it possible to cast from one interface to another when both interface’s signatures are same? The below source is giving the Unable to cast object of type 'ConsoleApplication1.First' to type 'ConsoleApplication1.ISecond'. exception.
class Program
{
static void Main(string[] args)
{
IFirst x = new First();
ISecond y = (ISecond)x;
y.DoSomething();
}
}
public interface IFirst
{
string DoSomething();
}
public class First : IFirst
{
public string DoSomething()
{
return "done";
}
}
public interface ISecond
{
string DoSomething();
}
No. They’re completely different types as far as the CLR and C# are concerned.
You could create a “bridge” type which wraps an implementation of
IFirstand implementsISecondby delegation, or vice versa.