Read the syntax
Resolve interface member conflicts and expose an operation through its intended contract.
Each cast selects the intended interface contract for the call.
What it is and why to use it
Two interfaces can contain the same member name with different meanings. Explicit interface implementation lets one class supply distinct implementations. It can also keep an interface-specific operation off the class’s ordinary public member surface. This is an API design choice, not a security boundary.
How it works
Prefix the member with the interface name and omit an access modifier. Call that member through a reference or cast to the interface. A matching public method can satisfy multiple interfaces when one shared meaning is correct; explicit implementations are needed only when that is unsuitable.
Common mistakes and limitations
Do not expect an explicitly implemented member to appear as a callable public method on the concrete type. Casting does not create a new object. An explicit interface does not hide sensitive behaviour from callers who can obtain the interface reference.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
void IResettable.Reset() { /* contract-specific implementation */ }Examples
Separate two same-named operations
Give two contracts their own label on the same object.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
var item = new Document();
Console.WriteLine(((IScreenLabel)item).Label());
Console.WriteLine(((IExportLabel)item).Label());
interface IScreenLabel { string Label(); }
interface IExportLabel { string Label(); }
class Document : IScreenLabel, IExportLabel {
string IScreenLabel.Label() => "Screen title";
string IExportLabel.Label() => "Export title";
}
- The interface-qualified names distinguish two members with identical signatures.
- Each cast selects the intended interface contract for the call.
- Both references point at the same Document instance. Calling item.Label() directly would not find either explicit implementation.
- Expected output: Screen title Export title
- Use explicit implementations when contracts need distinct meanings.
Expose a contract-specific reset
Offer ordinary methods on the class and reset through an interface.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
var counter = new Counter();
counter.Increment();
((IResettable)counter).Reset();
Console.WriteLine(counter.Value);
interface IResettable { void Reset(); }
class Counter : IResettable {
public int Value { get; private set; }
public void Increment() => Value++;
void IResettable.Reset() => Value = 0;
}
- Increment changes the value through the concrete class API.
- The explicit reset is accessible through IResettable and restores zero.
- This can reduce accidental use in a particular API surface, but callers with the interface can still invoke it.
- Expected output: 0
- Use explicit implementations when contracts need distinct meanings.
Best practices
- Use explicit implementations when contracts need distinct meanings.
- Keep interface-specific access intentional and documented.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference