--- url: "https://refactoring.guru/design-patterns/adapter/csharp/example" titulek: "Adapter in C# / Design Patterns" h1: "Adapter in C#" popis: "Adapter pattern in C#. Full code example in C# with detailed comments and explanation. Adapter is a structural design pattern, which allows incompatible objects to collaborate." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/adapter/csharp/example" drobecky: ["Home", "Design Patterns", "Adapter", "C#"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:27:12" znaku: 2443 slov: 344 kodovani: "utf-8" sha256: "d7b3d0ff1b9fcb5538077deac8c03451f895966f23b43286d5492dfe76afddc9" --- # **Adapter** in C# **Adapter** is a structural design pattern, which allows incompatible objects to collaborate. The Adapter acts as a wrapper between two objects. It catches calls for one object and transforms them to format and interface recognizable by the second object. [Learn more about Adapter](/design-patterns/adapter) **Complexity:** **Popularity:** **Usage examples:** The Adapter pattern is pretty common in C# code. It’s very often used in systems based on some legacy code. In such cases, Adapters make legacy code work with modern classes. **Identification:** Adapter is recognizable by a constructor which takes an instance of a different abstract/interface type. When the adapter receives a call to any of its methods, it translates parameters to the appropriate format and then directs the call to one or several methods of the wrapped object. ## Conceptual Example This example illustrates the structure of the **Adapter** design pattern. It focuses on answering these questions: - What classes does it consist of? - What roles do these classes play? - In what way the elements of the pattern are related? #### **Program.cs:** Conceptual example ``` using System; namespace RefactoringGuru.DesignPatterns.Adapter.Conceptual { // The Target defines the domain-specific interface used by the client code. public interface ITarget { string GetRequest(); } // The Adaptee contains some useful behavior, but its interface is // incompatible with the existing client code. The Adaptee needs some // adaptation before the client code can use it. class Adaptee { public string GetSpecificRequest() { return "Specific request."; } } // The Adapter makes the Adaptee's interface compatible with the Target's // interface. class Adapter : ITarget { private readonly Adaptee _adaptee; public Adapter(Adaptee adaptee) { this._adaptee = adaptee; } public string GetRequest() { return $"This is '{this._adaptee.GetSpecificRequest()}'"; } } class Program { static void Main(string[] args) { Adaptee adaptee = new Adaptee(); ITarget target = new Adapter(adaptee); Console.WriteLine("Adaptee interface is incompatible with the client."); Console.WriteLine("But with adapter client can call it's method."); Console.WriteLine(target.GetRequest()); } } } ``` #### **Output.txt:** Execution result ``` Adaptee interface is incompatible with the client. But with adapter client can call it's method. This is 'Specific request.' ``` ## **Adapter** in Other Languages