1

I'm trying to use lambda expression in C#:

using System;

namespace ConsoleApplication1 {
   public struct Point {
      public int x;
      public int y;

      public Point( int x, int y ) {
         this.x = x;
         this.y = y;
      }
   }

   public interface Mobile {
      Point getPosition();
   }

   public class Program {
      public void mth( Mobile mobile ) {
         Point p = mobile.getPosition();
         Console.WriteLine( "{ " + p.x + ", " + p.y + " }" );
      }

      static void Main( string[] args ) {
         new Program().mth( () => { return new Point( 4, 5 ); } ); <<<<<<<< ERROR
      }
   }
}

Compiler error (in French):

Impossible de convertir expression lambda en type 'ConsoleApplication1.Mobile', car il ne s'agit pas d'un type délégué

International English Translation:

Unable to convert lambda expression to type 'ConsoleApplication1.Mobile', because isn't a delegate type

What's the right syntax?

2
  • Are you trying to implement that interface method using a lambda? Commented Nov 19, 2013 at 12:25
  • I'm not sure what you are trying to do here - it looks like you want to getPosition() from Mobile but you are trying to create one using an anonymous function that returns a Point... can you explain what you are trying to do? Commented Nov 19, 2013 at 12:27

2 Answers 2

4

You appear to be confusing an interface and a delegate.

An interface is, roughly speaking, a collection of one or more methods. It has to be implemented by a class that provides implementations for each method; you can't build an implementation up from lambda methods.

A delegate is basically a named method signature. You can convert a lambda method to a delegate as long as the signature is correct, or you can create a delegate pointing at a method on a class, say.

I think what you're after to make your code work is to turn your Mobile into a delegate:

// This can represent any function that takes nothing and returns a point.
public delegate Point Mobile();

public class Program {

    // This takes any "Mobile" function, calls it and displays the result.      
    public void mth(Mobile getPosition) {
        Point p = getPosition();
        Console.WriteLine("{ " + p.x + ", " + p.y + " }");
    }

    // This calls mth with a lambda that matches the "Mobile" definition.
    static void Main( string[] args ) {
        new Program().mth(() => { return new Point( 4, 5 ); }); 
    }

    // You could also explicitly create a "Mobile".
    static void Main2( string[] args ) {
        Mobile myMobile = new Mobile(() => { return new Point( 4, 5 ); });
        new Program().mth(myMobile); 
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm pretty sure this is it.
Yes! That's THE answer! The concept of delegate is newer for me.
-1
   public class Mobile {
      public Func<Point> GetPosition { get; set; }
   }

   public class Program {
      public void mth( Mobile mobile ) {
         Point p = mobile.GetPosition();
         Console.WriteLine( "{ " + p.x + ", " + p.y + " }" );
      }

      static void Main( string[] args ) {
         new Program().mth( new Mobile { GetPosition = () => { return new Point( 4, 5 ); } } ); 
      }
   }

1 Comment

Sorry, I forgot adding getter-setter for GetPosition property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.