Are there any official ways to write an Immediately Invoked Function Expression?

后端 未结 6 1397
自闭症患者
自闭症患者 2021-02-04 02:21

Something like this:

var myObject = new MyClass()
{
    x = \" \".Select(y =>
    {
        //Do stuff..
        if (2 + 2 == 5)
            return \"I like c         


        
6条回答
  •  情书的邮戳
    2021-02-04 02:44

    Maybe not quite answering the question but ... one reason I wanted IIFE in C# was to give better meaning to a function being called on an object. In this example I wanted to give meaning as to what the 'Click' method does in a particular context within test code (and I do not like comments).

    Before ...

    lastLine.Click(); //click to begin editing line
    

    After ...

    Action clickToStartEditing = line => line.Click();
    clickToStartEditing(lastLine);
    

    Later I discovered I could simply create a new method with another name ...

    Action clickToStartEditing = lastLine.Click;
    clickToStartEditing(lastLine);
    

    Finally simply naming the variable in a better way can better solve the issue (for me)

    lastLineForEditing.Click();
    

    This solved an IIFE issue for me, to give meaning to the method name.

提交回复
热议问题