Action object

This is a discussion on Action object within the C# Programming forums, part of the General Programming Boards category; consider this code: Code: private void DoIf(bool b, Action f) { if (b) f(); } int x = 0; bool ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    Action object

    consider this code:

    Code:
            private void DoIf(bool b, Action f)
            {
                if (b)
                    f();
            }
    
    int x = 0;
    bool no = false;
    
    DoIf(no, () => x = 10);
    Does the compiler emit code to create a new action when the function is called or only when f() is actually called?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Are you asking about the CLR or the compiler? The compiler is going to do its thing independently of any runtime constraints such as branching.
    If you understand what you're doing, you're not learning anything.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    The Action object is syntactic sugar for a delegate taking one argument and returning void. The CLR will defined the object as a sealed class deriving from System.MulticastDelegate and define three public member functions; Invoke, BeginIvoke and EndInvoke. The CIL will call these member functions whenever you use the object.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very strange file action
    By JayCee++ in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2012, 06:01 AM
  2. When does an action exist?
    By Yarin in forum General Discussions
    Replies: 18
    Last Post: 10-18-2009, 01:50 PM
  3. Action Game - AI Contest
    By Queatrix in forum Contests Board
    Replies: 4
    Last Post: 01-18-2007, 05:55 PM
  4. action whilst downloading
    By eth0 in forum C Programming
    Replies: 5
    Last Post: 05-05-2005, 03:58 AM
  5. how do I perform an action over and over...
    By Rune Hunter in forum C# Programming
    Replies: 16
    Last Post: 02-25-2005, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21