0

I have trouble understanding lambdas, delegates and so on, I hope with someone giving me a solution to my problem I am able to understand those better. Basically it is possible to create (or change) the body of a method when an object of the class is initialized, no? Kinda like this:

Let's say I have 1 classes: Class A, which looks like this:

public class ClassA{
   int i;

   public ClassA(int number)
   {
   i = number;
   }

   public void Foo(){}
}

For demonstration purposes very minimalistic, now I also have somewhere else the static main, and what I want to do there is following: Creating multiple objects of ClassA and make it so that when I call ClassA.Foo I get different results I can determine myself, how is it supposed to look Syntax wise?

static void Main(string[] args)
{
    ClassA FooBlue = New ClassA(1){
                     public void Foo()
                     {
                     System.Console.WriteLine("I am a Fooranger Blue!");
                     };

    ClassA FooPink = New ClassA(2){
                     public void Foo()
                     {
                     System.Console.WriteLine("My  color is the manliest!");
                     };
    ...

So now when I do this:

    ...
    FooBlue.Foo();
    FooPink.Foo();
    System.Console.ReadLine();
}

I get following output on the console:

"I am a Fooranger Blue!"
"My color is the manliest!"

I just mention again that this is an example and by no means anything out of praxis but for the purpose of me understanding that stuff it would be great if someone can provide an answer that gives the desired solution, including the useless integer i.

Skyswimsky
  • 71
  • 2
  • 9
  • 2
    *Please* put more time into formatting your code before you post. This looks horrible, and you could easily have seen that before you posted. Other people are likely to be more willing to put effort into helping you if you put effort into your questions. Additionally, your code doesn't have anything to do with delegates or lambda expressions as far as I can tell. You certainly haven't *included* a delegate or a lambda expression yet... – Jon Skeet Jul 31 '14 at 14:42
  • @JonSkeet I changed my formatting (and added a small sentence to clarify what exactly I want) hope it is better now. And obviously it does not compile because it is wrong and I would like to know how I should do it right. – Skyswimsky Jul 31 '14 at 15:15
  • That formatting still doesn't look right to me - look at your first code snippet... no class declaration, and the indentation is still messed up. Is that how you'd want the code to look in Visual Studio? – Jon Skeet Jul 31 '14 at 15:17
  • @JonSkeet Right I totally messed up the class declaration, I am sorry still new to this. – Skyswimsky Jul 31 '14 at 15:24

1 Answers1

2

To accomplish the goal of "providing the implementation of a method when constructing the type" you can indeed use delegates. Simply accept a delegate when constructing the object and invoke it when you want it to be executed:

public class ClassA
{
    private Action action;

    public ClassA(Action action)
    {
        this.action = action;
    }
    public void Foo()
    {
        action();
    }
}

The syntax for a lambda is different than the syntax for creating a named method from a class' definition:

var fooBlue = new ClassA(() => Console.WriteLine("I am a Fooranger Blue!"));
Servy
  • 202,030
  • 26
  • 332
  • 449
  • That helps, but what if I want to pass a parameter alongside Foo() or if Foo() would be "more complicated" with if-statements or loops? – Skyswimsky Jul 31 '14 at 15:17
  • @user3800990 If you have a parameter than close over those parameters in the body of your lambda and use them there. If you have more to do then you should generally refactor that code out into a new named method, but you can use a statement lambda if you need to. – Servy Jul 31 '14 at 15:19
  • 1
    @user3800990: Do you mean if you wanted `Foo` to accept a parameter which would then be passed onto your custom code? You'd use an `Action` or whatever instead. To be honest, I think you'd be a lot better off with a good book here... – Jon Skeet Jul 31 '14 at 15:19
  • Oh I see how it is, also got to work what I wanted to get to work. And yeah I should probably read a book or similiar. – Skyswimsky Jul 31 '14 at 15:55