在 C# 中使用委托时遇到问题
having trouble using delegates in c#
通过使用委托,我希望使用以下代码将 IEnumerable 项目中的数字 5 打印到屏幕上;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using extended;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IEnumerable<int> cities = new[] { 1, 2, 3, 4, 5 };
IEnumerable<int> query = cities.StartsWith(hello);
foreach (var item in query)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
static int hello(int x)
{
return x > 4 ? x : 0;
}
}
}
namespace extended
{
public static class A
{
public static IEnumerable<T> StartsWith<T>(this IEnumerable<T> input, inputdelegate<T> predicate)
{
foreach (var item in input)
{
if (item.Equals(predicate))
{
yield return item;
}
}
}
public delegate int inputdelegate<T>(T input);
}
}
代码编译没有任何错误,但没有在屏幕上显示输出。知道我可能哪里出错了吗?
您没有调用谓词。此外,inputdelegate 应该有一个 return 类型的 T。将您的代码更改为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using extended;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IEnumerable<int> cities = new[] { 1, 2, 3, 4, 5 };
IEnumerable<int> query = cities.StartsWith(hello);
foreach (var item in query)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
static int hello(int x)
{
return x > 4 ? x : 0;
}
}
}
namespace extended
{
public static class A
{
public static IEnumerable<T> StartsWith<T>(this IEnumerable<T> input, inputdelegate<T> predicate)
{
foreach (var item in input)
{
if (item.Equals(predicate(item)))
{
yield return item;
}
}
}
public delegate T inputdelegate<T>(T input);
}
}
更新:根据 AlexD 的评论,您应该考虑将测试更改为:
if (predicate(item))
并将您的代表更新为:
public delegate bool inputdelegate<T>(T input);
并将您的 Hello 函数更新为:
static bool hello(int x)
{
return x > 4;
}
通过使用委托,我希望使用以下代码将 IEnumerable 项目中的数字 5 打印到屏幕上;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using extended;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IEnumerable<int> cities = new[] { 1, 2, 3, 4, 5 };
IEnumerable<int> query = cities.StartsWith(hello);
foreach (var item in query)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
static int hello(int x)
{
return x > 4 ? x : 0;
}
}
}
namespace extended
{
public static class A
{
public static IEnumerable<T> StartsWith<T>(this IEnumerable<T> input, inputdelegate<T> predicate)
{
foreach (var item in input)
{
if (item.Equals(predicate))
{
yield return item;
}
}
}
public delegate int inputdelegate<T>(T input);
}
}
代码编译没有任何错误,但没有在屏幕上显示输出。知道我可能哪里出错了吗?
您没有调用谓词。此外,inputdelegate 应该有一个 return 类型的 T。将您的代码更改为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using extended;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IEnumerable<int> cities = new[] { 1, 2, 3, 4, 5 };
IEnumerable<int> query = cities.StartsWith(hello);
foreach (var item in query)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
static int hello(int x)
{
return x > 4 ? x : 0;
}
}
}
namespace extended
{
public static class A
{
public static IEnumerable<T> StartsWith<T>(this IEnumerable<T> input, inputdelegate<T> predicate)
{
foreach (var item in input)
{
if (item.Equals(predicate(item)))
{
yield return item;
}
}
}
public delegate T inputdelegate<T>(T input);
}
}
更新:根据 AlexD 的评论,您应该考虑将测试更改为:
if (predicate(item))
并将您的代表更新为:
public delegate bool inputdelegate<T>(T input);
并将您的 Hello 函数更新为:
static bool hello(int x)
{
return x > 4;
}