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

  • Home
  • SEARCH
  • 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 925241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:29:22+00:00 2026-05-15T19:29:22+00:00

I’ve downloaded the VCSharpSample pack from Microsoft and started reading on Anonymous Delegates. I

  • 0

I’ve downloaded the VCSharpSample pack from Microsoft and started reading on Anonymous Delegates. I can more or less understand what the code is doing, but I don’t understand the reason behind it. Maybe if you gave me some examples where it would result in cleaner code and easier maintainability then I could wrap my head around it. 🙂

Can you help?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    delegate decimal CalculateBonus(decimal sales);

    class Player
    {
        public string Name;
        public decimal Score;
        public decimal Bonus;
        public CalculateBonus calculation_algorithm;
    }

    class Program
    {        
        static decimal calculateStandardBonus(decimal sales)
        {
            return sales / 10;
        }

        static void Main(string[] args)
        {
            decimal multiplier = 2;

            CalculateBonus standard_bonus = new CalculateBonus(calculateStandardBonus);
            CalculateBonus enhanced_bonus = delegate(decimal sales) { return multiplier * sales / 10; };

            Player[] players = new Player[5];

            for (int i = 0; i < 5; i++)
            {
                players[i] = new Player();
            }

            players[0].Name = "Sergio";
            players[0].Score = 240;
            players[0].calculation_algorithm = standard_bonus;

            players[1].Name = "Sergio";
            players[1].Score = 240;
            players[1].calculation_algorithm = enhanced_bonus;

            players[2].Name = "Caro";
            players[2].Score = 89;
            players[2].calculation_algorithm = standard_bonus;

            players[3].Name = "Andy";
            players[3].Score = 38;
            players[3].calculation_algorithm = enhanced_bonus;

            players[4].Name = "Hugo";
            players[4].Score = 600;
            players[4].calculation_algorithm = enhanced_bonus;

            foreach (Player player in players)
            {
                PerformCalculationBonus(player);
            }

            foreach (Player player in players)
            {
                DisplayPersonalDetails(player);
            }

            Console.ReadLine();
        }

        public static void PerformCalculationBonus(Player player)
        {
            player.Bonus = player.calculation_algorithm(player.Score);
        }

        public static void DisplayPersonalDetails(Player player)
        {
            Console.WriteLine(player.Name);
            Console.WriteLine(player.Score);
            Console.WriteLine(player.Bonus);
            Console.WriteLine("---------------");
        }
    }
}
  • 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-15T19:29:23+00:00Added an answer on May 15, 2026 at 7:29 pm

    Anonymous delegates are designed to help you make code more readable by being able to define the behavior of a simple delegate inline in another method. This means that if you’re dealing with something that requires a delegate (an event handler, for example), you can define the behavior right in the code rather than creating a dedicated function for it.

    In addition, they’re the precursor for lambda expressions. Things like LINQ to Objects (any of the methods that operate on IEnumerable<T>) use delegates to perform queries on objects. For example, if you have a collection of strings and you want a query that finds all of them that are five characters long, you can do that with a lambda:

    List<string> strings = ...
    
    var query = strings.Where(s => s.Length == 5);
    

    Or you could do it with an anonymous delegate:

    var query = strings.Where(delegate(string s) { return s.Length == 5; });
    

    If you didn’t have these, your code would look something like this:

    var query = strings.Where(IsFiveCharacters);
    
    ...
    
    private bool IsFiveCharacters(string input)
    {
        return input.Length == 5;
    }
    

    It’s important to realize, though, that lambdas and anonymous delegates are just compiler features. When your code is compiled, it does actually create regular functions like in the last example, but they’re hidden and named using characters that are illegal in the language being used. There’s a lot of logic that goes around them when doing things like closures (where you access a variable that exists outside of the lambda/anonymous delegate declaration), as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
In my XML file chapters tag has more chapter tag.i need to display chapters

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.