嘗試創建一個函式,我可以在其中傳遞不同的函式來測驗運行并測量它需要多長時間。現在主要問題是我什至無法通過函式..
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;
}
uj5u.com熱心網友回復:
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;
}
}
}
您需要在 Func 中指定要回傳的型別,請參閱更新的代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/462156.html
