<tfoot id='xHbL4'></tfoot>

<i id='xHbL4'><tr id='xHbL4'><dt id='xHbL4'><q id='xHbL4'><span id='xHbL4'><b id='xHbL4'><form id='xHbL4'><ins id='xHbL4'></ins><ul id='xHbL4'></ul><sub id='xHbL4'></sub></form><legend id='xHbL4'></legend><bdo id='xHbL4'><pre id='xHbL4'><center id='xHbL4'></center></pre></bdo></b><th id='xHbL4'></th></span></q></dt></tr></i><div id='xHbL4'><tfoot id='xHbL4'></tfoot><dl id='xHbL4'><fieldset id='xHbL4'></fieldset></dl></div>

  1. <small id='xHbL4'></small><noframes id='xHbL4'>

    <legend id='xHbL4'><style id='xHbL4'><dir id='xHbL4'><q id='xHbL4'></q></dir></style></legend>
        <bdo id='xHbL4'></bdo><ul id='xHbL4'></ul>

      如何在我的 List&lt;T&gt; 中形成一个好的谓词委托来 Find() 某些东西?

      How do I form a good predicate delegate to Find() something in my Listlt;Tgt;?(如何在我的 Listlt;Tgt; 中形成一个好的谓词委托来 Find() 某些东西?)

        <legend id='6w8fh'><style id='6w8fh'><dir id='6w8fh'><q id='6w8fh'></q></dir></style></legend>
          <tbody id='6w8fh'></tbody>

            • <bdo id='6w8fh'></bdo><ul id='6w8fh'></ul>
            • <i id='6w8fh'><tr id='6w8fh'><dt id='6w8fh'><q id='6w8fh'><span id='6w8fh'><b id='6w8fh'><form id='6w8fh'><ins id='6w8fh'></ins><ul id='6w8fh'></ul><sub id='6w8fh'></sub></form><legend id='6w8fh'></legend><bdo id='6w8fh'><pre id='6w8fh'><center id='6w8fh'></center></pre></bdo></b><th id='6w8fh'></th></span></q></dt></tr></i><div id='6w8fh'><tfoot id='6w8fh'></tfoot><dl id='6w8fh'><fieldset id='6w8fh'></fieldset></dl></div>

              <small id='6w8fh'></small><noframes id='6w8fh'>

              • <tfoot id='6w8fh'></tfoot>
                本文介绍了如何在我的 List&lt;T&gt; 中形成一个好的谓词委托来 Find() 某些东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在查看 MSDN 之后,我仍然不清楚我应该如何使用 T 的成员变量(其中 T 是一个类)在 List 中使用 Find() 方法来形成一个正确的谓词

                After looking on MSDN, it's still unclear to me how I should form a proper predicate to use the Find() method in List using a member variable of T (where T is a class)

                例如:

                public class Car
                {
                   public string Make;
                   public string Model;
                   public int Year;
                }
                
                {  // somewhere in my code
                   List<Car> carList = new List<Car>();
                   // ... code to add Cars ...
                
                   Car myCar = new Car();
                
                   // Find the first of each car made between 1980 and 2000
                   for (int x = 1980; x < 2000; x++)
                   {
                       myCar = carList.Find(byYear(x));
                       Console.Writeline(myCar.Make + myCar.Model);
                   }
                }
                

                我的byYear"谓词应该是什么样的?

                What should my "byYear" predicate look like?

                (MSDN 示例仅讨论恐龙列表,仅搜索不变的值saurus"——它没有显示如何将值传递给谓词...)

                (The MSDN example only talks about a List of dinosaurs and only searches for an unchanging value "saurus" -- It doesn't show how to pass a value into the predicate...)

                我使用的是 VS2005/.NET2.0,所以我认为 Lambda 表示法对我不可用...

                I'm using VS2005/.NET2.0, so I don't think Lambda notation is available to me...

                在示例中删除了1999",因为我可能希望根据不同的值以编程方式查找".使用 for-do 循环将示例更改为从 1980 年到 2000 年的汽车范围.

                Removed "1999" in the example because I may want to "Find" programatically based on different values. Example changed to range of cars from 1980 to 2000 using for-do loop.

                推荐答案

                好的,在 .NET 2.0 中您可以使用委托,如下所示:

                Ok, in .NET 2.0 you can use delegates, like so:

                static Predicate<Car> ByYear(int year)
                {
                    return delegate(Car car)
                    {
                        return car.Year == year;
                    };
                }
                
                static void Main(string[] args)
                {
                    // yeah, this bit is C# 3.0, but ignore it - it's just setting up the list.
                    List<Car> list = new List<Car>
                    {
                        new Car { Year = 1940 },
                        new Car { Year = 1965 },
                        new Car { Year = 1973 },
                        new Car { Year = 1999 }
                    };
                    var car99 = list.Find(ByYear(1999));
                    var car65 = list.Find(ByYear(1965));
                
                    Console.WriteLine(car99.Year);
                    Console.WriteLine(car65.Year);
                }
                

                这篇关于如何在我的 List&lt;T&gt; 中形成一个好的谓词委托来 Find() 某些东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                  <tbody id='31GIK'></tbody>
              • <small id='31GIK'></small><noframes id='31GIK'>

              • <legend id='31GIK'><style id='31GIK'><dir id='31GIK'><q id='31GIK'></q></dir></style></legend>

                <tfoot id='31GIK'></tfoot>
                      <i id='31GIK'><tr id='31GIK'><dt id='31GIK'><q id='31GIK'><span id='31GIK'><b id='31GIK'><form id='31GIK'><ins id='31GIK'></ins><ul id='31GIK'></ul><sub id='31GIK'></sub></form><legend id='31GIK'></legend><bdo id='31GIK'><pre id='31GIK'><center id='31GIK'></center></pre></bdo></b><th id='31GIK'></th></span></q></dt></tr></i><div id='31GIK'><tfoot id='31GIK'></tfoot><dl id='31GIK'><fieldset id='31GIK'></fieldset></dl></div>
                        <bdo id='31GIK'></bdo><ul id='31GIK'></ul>