奥凯,
我有一个委托方法和一个过滤器,我传递了委托。然后在Main我返回方法。
但我的问题是:如何在没有委托的情况下编写这些内容?
这就是我所拥有的:
public class ProcessData
{
public Int32 Id;
public Int64 Memory;
public String Name;
}
delegate bool Predicate<T>(T obj);
class Program
{
static void DisplayProcesses(Predicate<Process> match)
{
var processes = new List<ProcessData>();
foreach (var process in Process.GetProcesses())
{
if (match(process))
{
processes.Add(new ProcessData
{
Id = process.Id,
Name = process.ProcessName,
Memory = process.WorkingSet64
});
}
}
Console.WriteLine();
}
static Boolean Filter(Process proces)
{
return proces.WorkingSet64 >= 20 * 1021 * 1021;
}
}
//主要方法:
static void Main(string[] args)
{
DisplayProcesses(Filter);
}
谢谢
答案 0 :(得分:1)
要编写没有委托的代码,只需将谓词从过滤器中删除
proces.WorkingSet64 >= 20 * 1021 * 1021;
并从方法中删除参数
这里如何
static void DisplayProcesses()
{
var processes = new List<ProcessData>();
foreach (var process in Process.GetProcesses())
{
if (process.WorkingSet64 >= 20 * 1021 * 1021;)
{
processes.Add(new ProcessData
{
Id = process.Id,
Name = process.ProcessName,
Memory = process.WorkingSet64
});
}
}
Console.WriteLine();
}