Home > Blockchain >  Passing Function as Parameter ( CS1503 )
Passing Function as Parameter ( CS1503 )

Time:04-15

Trying to create a function in which I can pass different functions for it to test run and measure how long did it take. Now the main problem is I can't even pass the function..

public static void Main(string[] args)
        {
            string Inputs = Console.ReadLine();
            List<int> UnSorted = new List<int>();

            for (int i = 0; i < Inputs.Split(' ').Count(); i  )
            {
                UnSorted.Add(int.Parse(Inputs.Split(' ')[i]));
            }

            CheckTimeOfSorting(SortWithTwoLoops); // <--- Error ( Can't convert from method group to Func<List<int>>)

            Console.WriteLine(String.Join(" ", UnSorted));
        }

        public static void CheckTimeOfSorting(Func<List<int>> SortingFunc)
        {

        }

        public static List<int> SortWithTwoLoops(List<int> UnSorted)
        {
            List<int> Result = UnSorted;
            for (int i = 0; i < Result.Count; i  )
            {
                for (int j = i   1; j < Result.Count; j  )
                {
                    if (Result[i] > Result[j])
                    {
                        int temp1 = Result[i];
                        int temp2 = Result[j];

                        Result[i] = temp2;
                        Result[j] = temp1;
                    }
                }
            }
            return Result;
        }

CodePudding user response:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace NewVSFunction
{
    public static class MykytaHttpFunction
    {
        public static void Main(string[] args)
        {
            string Inputs = Console.ReadLine();
            List<int> UnSorted = new List<int>();

            for (int i = 0; i < Inputs.Split(' ').Count(); i  )
            {
                UnSorted.Add(int.Parse(Inputs.Split(' ')[i]));
            }

            CheckTimeOfSorting(SortWithTwoLoops); // <--- Error ( Can't convert from method group to Func<List<int>>)

            Console.WriteLine(String.Join(" ", UnSorted));
        }

        public static void CheckTimeOfSorting(Func<List<int>, List<int>> SortingFunc)
        {

        }

        public static List<int> SortWithTwoLoops(List<int> UnSorted)
        {
            List<int> Result = UnSorted;
            for (int i = 0; i < Result.Count; i  )
            {
                for (int j = i   1; j < Result.Count; j  )
                {
                    if (Result[i] > Result[j])
                    {
                        int temp1 = Result[i];
                        int temp2 = Result[j];

                        Result[i] = temp2;
                        Result[j] = temp1;
                    }
                }
            }
            return Result;
        }
    }
}

you need to specify an type to return in Func, see updated code

  • Related