Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Defining Delegates | Interactive GUI
C# Desktop Development with .NET MAUI

Challenge: Defining Delegates

Svep för att visa menyn

You can download the base code by cloning the Github Repository.

Your task is to define a delegate type called Operation, which can refer to the methods that have the same signature as the max and avg methods.

Note
Note

Currently there is no delegate definition so the program doesn't compile, however after defining the delegate type you should be able to successfully compile and run the program, indicating that your solution is correct.

Hint
expand arrow

Use the syntax delegate <return type> <type name> <parameter list> to define the delegate type.

Solution
expand arrow
using System.ComponentModel.DataAnnotations;

namespace DelegateIntro
{
    internal class Program
    {
        // Write code below this line
        delegate float Operation(int[] nums);
        // Write code above this line

        static void Main(string[] args)
        {
            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Console.WriteLine(arrayMethod(max, nums));
            Console.WriteLine(arrayMethod(avg, nums));
        }

        static float arrayMethod(Operation opr, int[] nums)
        {
            return opr(nums);
        }
        
        static float max(int[] nums)
        {
            int max = nums[0];
            for(int i = 1; i &lt; nums.Length; i++)
                if (max &lt; nums[i])
                    max = nums[i];
            return max;
        }

        static float avg(int[] nums)
        {
            float sum = 0;
            for(int i = 0; i &lt; nums.Length; i++)
                sum += nums[i];
            return sum / nums.Length;
        }
    }
}    
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Challenge: Defining Delegates

You can download the base code by cloning the Github Repository.

Your task is to define a delegate type called Operation, which can refer to the methods that have the same signature as the max and avg methods.

Note
Note

Currently there is no delegate definition so the program doesn't compile, however after defining the delegate type you should be able to successfully compile and run the program, indicating that your solution is correct.

Hint
expand arrow

Use the syntax delegate <return type> <type name> <parameter list> to define the delegate type.

Solution
expand arrow
using System.ComponentModel.DataAnnotations;

namespace DelegateIntro
{
    internal class Program
    {
        // Write code below this line
        delegate float Operation(int[] nums);
        // Write code above this line

        static void Main(string[] args)
        {
            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Console.WriteLine(arrayMethod(max, nums));
            Console.WriteLine(arrayMethod(avg, nums));
        }

        static float arrayMethod(Operation opr, int[] nums)
        {
            return opr(nums);
        }
        
        static float max(int[] nums)
        {
            int max = nums[0];
            for(int i = 1; i &lt; nums.Length; i++)
                if (max &lt; nums[i])
                    max = nums[i];
            return max;
        }

        static float avg(int[] nums)
        {
            float sum = 0;
            for(int i = 0; i &lt; nums.Length; i++)
                sum += nums[i];
            return sum / nums.Length;
        }
    }
}    
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2
some-alt