Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1026975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:10:02+00:00 2026-05-16T12:10:02+00:00

You can do anonymous functions in C# like you can in JavaScript: JavaScript: var

  • 0

You can do anonymous functions in C# like you can in JavaScript:

JavaScript:

var s = (function ()
{
    return "Hello World!";
}());

C#:

var s = new Func<String>(() =>
{
    return "Hello World!";
})();

… In JavaScript you can pass functions to be executed by other functions. On top of that; you can pass parameters to the function which gets executed:

var f = function (message) // function to be executed
{
    alert(message);
};

function execute(f) // function executing another function
{
    f("Hello World!"); // executing f; passing parameter ("message")
}

Is the above example possible in C#?

Update

Use-case: I am iterating over a bunch of database, logging specific entities. Instead of calling my second function (F()) inside Log() of Logger, I’d like to call F() outside the class.

… Something along the lines of:

public void F(String databaseName)
{
}

public class Logger
{
    public void Log(Function f)
    {
        var databaseName = "";

        f(databaseName);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:10:03+00:00Added an answer on May 16, 2026 at 12:10 pm

    Absolutely – you just need to give the method an appropriate signature:

    public void Execute(Action<string> action)
    {
        action("Hello world");
    }
    ...
    Execute(x => Console.WriteLine(x));
    

    Note that you do have to specify the particular delegate type in the parameter – you can’t just declare it as Delegate for example.

    EDIT: Your database example is exactly the same as this – you want to pass in a string and not get any output, which is exactly what Action<string> does. Except if you’re trying to call an existing method (F() in your code) you don’t even need a lambda expression – you can use method group conversions instead:

    public void F(String databaseName)
    {
    }
    
    public class Logger
    {
        public void Log(Action<string> f)
        {
            var databaseName = "";
    
            f(databaseName);
        }
    }
    
    // Call it like this:
    Logger logger = new Logger(...);
    logger.Log(F);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In JavaScript, you can define anonymous functions that are executed immediately: (function () {
I've got JavaScript code like this => (function(){ document.getElementById(element).onclick = function(){ var r =
I can use the following to create a function dynamically in javascript: var MyClass
How can I define anonymous functions in python, where the bahaviour should depend on
Summary Can you explain the reasoning behind the syntax for encapsulated anonymous functions in
Is there a way I can get this array walk with my anonymous function
How can I create an anonymous and curried function in Scala? The following two
Using the C# object initializer syntax I can instantiate an anonymous object like this:
I'd like to assign anonymous functions to arrays in PHP inside of a class,
Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.