Two callers can agree on a method signature and still disagree about its meaning. A metric consumer expects GetDistance() to return meters; an imperial consumer expects feet. One Runway can satisfy both contracts by implementing each member explicitly.
This tutorial extends the basic contract syntax in Lesson 13: Interfaces. You will call the same object through two interface types, check four runway lengths, and reproduce the compiler error that prevents calling either explicit member directly on the class.
Same signature, different promises
A single public method with a matching signature can implement both interfaces. That is useful when both contracts mean the same thing. Here it would silently give one caller the wrong unit. Microsoft’s explicit implementation guide explains how qualifying the member with its interface name gives each contract a separate implementation.
The example follows the metric/imperial scenario in Microsoft’s interface fundamentals guide, adding a complete console runner and fixed expected results. INamed supplies each output label. Its ordinary public Name property illustrates that one class can mix implicit and explicit implementations.
Run the complete program
Use a .NET 10 SDK. The listing was verified targeting net10.0 with the SDK’s default C# language version, nullable analysis, implicit usings and warnings treated as errors. Explicit interface implementation is an established C# feature; it is not new to .NET 10. No external packages are needed.
dotnet new console --framework net10.0 --name RunwayContracts
cd RunwayContracts
Replace the entire Program.cs with this listing, then run dotnet run.
using System;
using System.Globalization;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var cases = new (string Name, decimal Meters, decimal ExpectedFeet)[]
{
("A", 100m, 328.084m),
("B", 800m, 2624.672m),
("C", 1250m, 4101.050m),
("D", 2400m, 7874.016m)
};
int assertions = 0;
foreach (var item in cases)
{
var runway = new Runway(item.Name, item.Meters);
// runway.GetDistance(); // CS1061: no directly accessible class member.
IMetric metric = (IMetric)runway;
IImperial imperial = (IImperial)runway;
INamed named = (INamed)runway;
decimal meters = metric.GetDistance();
decimal feet = imperial.GetDistance();
Check(meters, item.Meters, "metric", named.Name);
Check(feet, item.ExpectedFeet, "imperial", named.Name);
assertions += 2;
Console.WriteLine($"{named.Name}: {meters:F3} m | {feet:F3} ft");
}
Console.WriteLine($"Passed {assertions} distance assertions.");
static void Check(decimal actual, decimal expected, string contract, string name)
{
if (actual != expected)
throw new InvalidOperationException(
$"{name} {contract}: expected {expected}, got {actual}.");
}
interface IMetric
{
decimal GetDistance(); // Meters.
}
interface IImperial
{
decimal GetDistance(); // Feet, using this example's fixed conversion factor.
}
interface INamed
{
string Name { get; }
}
sealed class Runway : IMetric, IImperial, INamed
{
private const decimal FeetPerMeter = 3.28084m;
private readonly decimal meters;
public Runway(string name, decimal meters)
{
Name = name;
this.meters = meters;
}
public string Name { get; }
decimal IMetric.GetDistance() => meters;
decimal IImperial.GetDistance() => meters * FeetPerMeter;
}
Expected output:
A: 100.000 m | 328.084 ft
B: 800.000 m | 2624.672 ft
C: 1250.000 m | 4101.050 ft
D: 2400.000 m | 7874.016 ft
Passed 8 distance assertions.
Follow the interface at the call site
Each loop iteration creates one runway. The casts to IMetric, IImperial and INamed give callers different views of that same instance. They do not create three runway objects. Calling through metric selects the implementation qualified by IMetric; calling through imperial selects the one qualified by IImperial.
There is no public GetDistance method on this class. The commented direct call deliberately records that boundary. In a separately compiled variant, removing that line’s comment produced CS1061: Runway has no accessible GetDistance member. Keep it commented for the successful run. The explicit implementations also have no public or private modifier, as required by the explicit implementation rules.
The public Name property remains available through both the class and INamed. You only need explicit implementation for members whose contract-specific behavior or visibility calls for it.
What the eight checks establish
For each runway, one assertion checks meters and one checks feet. The four imperial expectations are stored as literals calculated in advance from the example’s fixed factor, 3.28084; the assertions do not recompute the implementation’s multiplication. Returning meters from both interfaces would fail the imperial checks. Swapping the implementations would fail the metric checks.
decimal keeps these particular finite products exact, so the assertions use equality. The three-decimal formatting controls display only. The fixed factor is an illustrative approximation for this exercise, not a precision standard for aviation software. The example assumes the four supplied positive lengths and does not implement input validation. A failed assertion throws before the success message; the verified run completed with exit code zero.
Use explicit implementation when you must keep both contracts
If you control a new API, names such as GetMeters and GetFeet make the unit visible without inspecting the receiver’s type. Prefer that clarity when callers can adopt it. Explicit implementation is useful when existing interfaces already require the same name with different meanings, or when a member should be reached through its interface rather than the class’s public surface.
Do not choose it merely because a class implements several interfaces. For a different use of multiple contracts, try building a text adventure with small capability interfaces. Those interfaces describe separate abilities; this example handles two meanings of the same method signature.