none
How can a make temporary function without name which can not stored RRS feed

  • Question

  • Hi,

    Have a nice day!!!

    I want to know that in C# Program, How can a make temporary function without name which can not stored but whenever i need  i can use it again?

    Anyone please help..

    Whether you agree or not, I value your consideration.


    Thursday, December 26, 2019 10:19 AM

Answers

  • Not sure what you mean by temporary function as functions must exist at compilation time otherwise they can not be called. So they cannot be "temporary". 

    If you want a function that isn't a method but you can call then look at using either a lambda or a local function. Both ultimately get compiled to private methods but as far as the compiler is concerned they can only be called by the code that has them within scope.

    //Lambda - only "callable" within current scope
    .Where(x => some condition)
    
    //Local function
    void Foo ()
    {
       LocalFunc();
    
       void LocalFunc () 
       {
       }
    }

    If you want to auto-generate and run code then you'll need to look into using CodeDOM, Roslyn or the reflection Emit API. In all cases you'll end up generating the C# code and running it through the compiler. You get back an assembly with the containing code that you can then run. It is temporary only in the sense that when the assembly goes away so does the code.

    Can you explain your situation a littler more so we can provide a more focused answer?


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Jack_tata Sunday, December 29, 2019 5:07 AM
    Thursday, December 26, 2019 2:49 PM
    Moderator
  • Hi Jack_tata,
    I viewed your description. Could you please explain in detail what the "temporary function" means?
    If your meaning is Anonymous functions, please refer to these documents.
    [Anonymous functions (C# Programming Guide)]
    [C# - Anonymous Method]
    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jack_tata Sunday, December 29, 2019 5:08 AM
    Friday, December 27, 2019 8:29 AM

All replies

  • Not sure what you mean by temporary function as functions must exist at compilation time otherwise they can not be called. So they cannot be "temporary". 

    If you want a function that isn't a method but you can call then look at using either a lambda or a local function. Both ultimately get compiled to private methods but as far as the compiler is concerned they can only be called by the code that has them within scope.

    //Lambda - only "callable" within current scope
    .Where(x => some condition)
    
    //Local function
    void Foo ()
    {
       LocalFunc();
    
       void LocalFunc () 
       {
       }
    }

    If you want to auto-generate and run code then you'll need to look into using CodeDOM, Roslyn or the reflection Emit API. In all cases you'll end up generating the C# code and running it through the compiler. You get back an assembly with the containing code that you can then run. It is temporary only in the sense that when the assembly goes away so does the code.

    Can you explain your situation a littler more so we can provide a more focused answer?


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Jack_tata Sunday, December 29, 2019 5:07 AM
    Thursday, December 26, 2019 2:49 PM
    Moderator
  • Hi Jack_tata,
    I viewed your description. Could you please explain in detail what the "temporary function" means?
    If your meaning is Anonymous functions, please refer to these documents.
    [Anonymous functions (C# Programming Guide)]
    [C# - Anonymous Method]
    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jack_tata Sunday, December 29, 2019 5:08 AM
    Friday, December 27, 2019 8:29 AM
  • You probably want to look at Anonymous Functions and/or Local Functions.


    william xifaras

    Friday, December 27, 2019 2:24 PM
  • I hope you can see the inherent contradiction between "can not stored" and "i can use it again".  Those two requirements cannot both be met.  You get one or the other.

    What is the overall goal you are trying to achieve here?  I suspect you simply misunderstand how functions are scoped, and the normal function process will serve you just fine.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, December 27, 2019 8:37 PM