---
title: "How to implement an interface with two abstract methods by a lambda expression?"  
description: "How to implement an interface with two abstract methods by a lambda expression?"  
author: "Sandra Emily"  
published: 2023-08-17  
updated: 2023-08-18  
canonical: https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression  
category: "c#"  
tags: ["c#", "functions", "lambda expression"]  
reading_time: 1 minute  

---

# How to implement an interface with two abstract methods by a lambda expression?

How to [implement an interface](https://www.mindstick.com/forum/161272/how-do-you-implement-an-interface-in-c-sharp) with two [abstract](https://www.mindstick.com/forum/156718/when-to-use-abstract-classes-in-c-sharp) [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) by a [lambda expression](https://www.mindstick.com/forum/159552/what-is-a-lambda-expression-in-c-plus-plus)?

## Replies

### Reply by Aryan Kumar

Sure, I can help you with that.

To implement an [interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces) with two abstract methods by a [lambda](https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp) [expression](https://www.mindstick.com/articles/1861/sqlite-expressions), you can use the `auto` keyword. The `auto` keyword tells the compiler to infer the type of the lambda expression.

The following code is an example of how to implement an interface with two abstract methods by a lambda expression:

C++

```plaintext
// Define an interface with two abstract methods.
interface IMyInterface {
  void Method1();
  void Method2();
};

// Implement the interface with a lambda expression.
auto myInterface = []() {
  // Implement Method1().
  void Method1() {
    cout << "Method1()" << endl;
  }

  // Implement Method2().
  void Method2() {
    cout << "Method2()" << endl;
  }
};

// Call the methods of the interface.
myInterface.Method1();
myInterface.Method2();
```

In this code, the `myInterface` variable is a lambda expression that implements the `IMyInterface` interface. The `Method1()` and `Method2()` methods of the `myInterface` variable are implemented by the lambda expression.


---

Original Source: https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
