-3

I have the following base class:

public class Baseclass
{
    public Baseclass(string anyparam)
    {
    }
}

I want to execute a lambda inside of the constructor of the child class:

public class Subclass : Baseclass
{
    public Subclass() : base(delegate() 
    {
        string returnstring;
        // Do Something
        return returnstring; 
    })
    {

    }
}

I don't want to waste any method on this, and I've not seen any example solving this without declaring the parameter type as func. How do I do this in C#?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
LuckyLikey
  • 3,504
  • 1
  • 31
  • 54
  • 1
    "waste any method on this" -> For 'performance' reasons or conciseness reasons? If the latter; the readability more than makes up for the 'value' in saving keystrokes. If the former, do you have benchmarks? If for another reason, could you enlighten us? What problem are you trying to solve? – George Stocker Mar 05 '15 at 14:19
  • See this: http://stackoverflow.com/questions/10763919/call-constructor-base-after-code-execution –  Mar 05 '15 at 14:20
  • 3
    This is a clear [XY-Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you *really* trying to achive, not how are you trying to achieve it. – Jamiec Mar 05 '15 at 14:20
  • 3
    What is wrong with a method? I like methods. – Patrick Hofman Mar 05 '15 at 14:21
  • 2
    What is the actual question here? – PiousVenom Mar 05 '15 at 14:21
  • Its just a simple *How do I do* to improve my knowledge of c# – LuckyLikey Mar 05 '15 at 14:32

2 Answers2

4

You don't have to declare parameter as Func, you can create Func instance inline and call it:

public class Subclass : Baseclass
{
    public Subclass()
        : base((new Func<string>(() =>
                                 {
                                     const string returnstring = "a";

                                     // Do Something

                                     return returnstring;
                                 })()))
    {

    }
}

It it looks very ugly, can produce problems and I don't recommend it to you.

  • It's also not 'normal' C#. Yes, it's possible, but it's not convention. I'd have to see the actual usecase the OP proposes to comment any more. But to internet visitors coming from another language: This is not recommended in C#. – George Stocker Mar 05 '15 at 14:26
  • Yes, indeed. It is possible, but like I posted - I don't recommed that snippet. I strongly believe that the OP have badly designed classes if he/she is looking for such a solution. –  Mar 05 '15 at 14:30
  • Nothing about bad desing, it's just improving knowledge. I just recently used a statement lambda inside a collection.where() which I have found very useful – LuckyLikey Mar 05 '15 at 14:36
  • @LuckyLikey Don't do this in code bases with other developers; they'll likely ask "why did you do this?" – George Stocker Mar 05 '15 at 14:54
  • @GeorgeStocker there seems to be a missunderstanding, I only want to know how it may be done. I know doing this would produce many "WTF" from my colleagues - **But anyway thank you for helping me** – LuckyLikey Mar 05 '15 at 15:00
2

I think the best you can do here is overload your base class constructor:

public class Baseclass
{
    public Baseclass(string anyparam)
    {

    }
    public Baseclass(Func<string> f):this(f())
    {

    }
}

public class Subclass : Baseclass
{
    public Subclass()
        : base(()=> 
                {
                    string returnstring="foo";

                    // Do Something

                    return returnstring; 
                })
    {

    }
}
spender
  • 117,338
  • 33
  • 229
  • 351