Note that there are some explanatory texts on larger screens.

plurals
  1. POC# interface method ambiguity
    text
    copied!<p>Consider the following example:</p> <pre><code>interface IBase1 { int Percentage { get; set; } } interface IBase2 { int Percentage { get; set; } } interface IAllYourBase : IBase1, IBase2 { } class AllYourBase : IAllYourBase { int percentage; int Percentage { get { return percentage; } set { percentage = value; } } } void Foo() { IAllYourBase iayb = new AllYourBase(); int percentage = iayb.Percentage; // Fails to compile. Ambiguity between 'Percentage' property. } </code></pre> <p>In the example above, there is ambiguity between which <code>Percentage</code> property to call. Assuming the <code>IBase1</code> and <code>IBase2</code> interfaces may <em>not</em> be changed, how would I go about resolving this ambiguity in the cleanest, most preferred way?</p> <h2>Update</h2> <p>Based on the responses I was getting for using explicit interface implementation, I want to mention that while this does solve the problem it does not solve it in an ideal way for me because I use my <code>AllYourBase</code> object as an <code>IAllYourBase</code> most of the time, never as an <code>IBase1</code> or <code>IBase2</code>. This is mostly because <code>IAllYourBase</code> also has interface methods (I failed to detail those in my code snippet above because I thought they were irrelevant) that are implemented by <code>AllYourBase</code> and I want to access those too. Casting back and forth all the time will get very tedious and result in messy code.</p> <p>I did try one solution that involved defining the <code>Percentage</code> property in <code>IAllYourBase</code> and not using explicit interface implementation, which seemed to get rid of the compiler error at least:</p> <pre><code>class IAllYourBase : IBase1, IBase2 { int Percentage { get; set; } } </code></pre> <p>Is this a valid solution?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload