Reputation: 17278
i try to call base.Alan(); in HacimBul. But base. dont give intellisense alan method
public double HacimBul()
{
throw new Exception();
//return base..... --> how can i see base.Alan();
}
namespace interfaceClass
{
class Program
{
static void Main(string[] args)
{
}
}
interface Ikenar
{
double kenar { get; set; }
}
interface Iyukseklik
{
double yuksekli {get; set;}
}
interface IAlan
{
double Alan();
}
interface IHacim
{
double Hacim();
}
class Alan : Ikenar, IAlan
{
public double kenar
{
get;
set;
}
double IAlan.Alan()
{
return kenar * kenar;
}
}
class Hacim : Alan, Iyukseklik
{
public double kenar
{
get;
set;
}
public double yuksekli
{
get;
set;
}
public double HacimBul()
{
throw new Exception();
//return base..... --> how can i see base.Alan();
}
}
}
Upvotes: 0
Views: 398
Reputation: 391326
The method is not public or protected, it is private the way you've defined it, and even more private than just private
as well, it is only available if you go through the interface.
This means that in order to call the method you will have to:
Change the explicit implementation of your Alan method to an implicit one (making the method public) in Alan:
public void Alan()
{
}
or, you will have to cast the instance during the call:
((IAlan)this).Alan();
Unfortunately, with that last syntax there, you won't actually be calling "base.Alan", but rather just "this.Alan", which might make a difference if you also override that method in the descendant class.
Here's an example:
using System;
namespace interfaceClass
{
public interface ITest
{
void Execute();
}
public class Base : ITest
{
void ITest.Execute()
{
Console.Out.WriteLine("Base.ITest.Execute");
}
}
public class Descendant : Base, ITest
{
void ITest.Execute()
{
Console.Out.WriteLine("Descendant.ITest.Execute");
}
public void Test()
{
// There's no way to call "base.Execute()" here
((ITest)this).Execute();
}
}
class Program
{
static void Main(string[] args)
{
Descendant d = new Descendant();
d.Test();
}
}
}
Upvotes: 1
Reputation: 1500215
No, it won't work at the moment. The base implementation is only available on an expression of type IAlan
, because of explicit interface implementation.
You can use this though:
return ((IAlan)this).Alan();
Or you can use implicit interface implementation in Alan
instead, although that will mean renaming the class or the method.
For others trying to get a grip on this problem, it's more simply expressed like this - in a way which doesn't reuse method names as class names:
public interface IFoo
{
void Bar();
}
public class BaseFoo : IFoo
{
void IFoo.Bar()
{
// Do something
}
}
public class DerivedFoo : BaseFoo
{
void OtherMethod()
{
// Doesn't compile due to explicit interface implementation
base.Bar();
// Will work
((IFoo)this).Bar();
}
}
Upvotes: 1
Reputation: 21742
You have made an explicit implementation of Alan so you have to options
either change the name of the method or the class called Alan so you dont have a name clash and implement the method as a normal method E.g
public double Alan() //name of class can't be Alan in this case
{
return kenar * kenar;
}
or
public double HacimBul()
{
return ((IAlan)this).Alan();
}
The reason way you don't get intellisense is that the method is only available when the obejct of the type Alan is declared as IAlan otherwise the Alan method will be hidden
Upvotes: 0
Reputation: 33476
double IAlan.Alan() // this is explicit interface implementation
{
return kenar * kenar;
}
By prefixing the method name with the interface, you are telling the user that the method Alan
will be usable only through a reference of the interface & not of the class.
Change the above to
double Alan()
{
return kenar * kenar;
}
inside Alan
class.
Upvotes: 0