我怎样才能找到一个List℃的特定元素; T&GT ;? [英] How can I find a specific element in a List<T>?

查看:72
本文介绍了我怎样才能找到一个List℃的特定元素; T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用的列表是这样的:

My application uses a list like this:

列表< MyClass的>名单=新名单,LT; MyClass的>();

使用添加方法 MyClass的添加到列表中的另一个实例。

Using the Add method, another instance of MyClass is added to the list.

MyClass的提供A.O。以下方法:

MyClass provides a.o. the following methods:

public void SetId(String Id);
public String GetId();

我如何才能找到使用 GETID 方法手段 MyClass的的一个具体实例?我知道还有就是查找方法,但我不知道这是否会在这里工作?!

How can I find a specific instance of MyClass by means of using the GetId method? I know there is the Find method but I don't know if this would work here?!

推荐答案

使用一个lambda前pression

Use a lambda expression

MyClass result = list.Find(x => x.GetId() == "xy");


注意:C#有特性的内置语法。而不是写设定和Get-方法,写


Note: C# has a built-in syntax for properties. Instead of writing Set- and Get-methods, write

private string _id;
public string Id
{
    get
    {
        return _id;
    }
    set
    {
        _id = value;
    }
}

仅在set访问已知的上下文关键字。它重新presents分配给该属性的值。

value is a contextual keyword known only in the set accessor. It represents the value assigned to the property.

由于这种模式经常使用,C#提供了自动实现的属性。他们均高于code短版;然而,支持变量是隐而不访问(这是从VB类中访问,不过)。

Since this pattern is often used, C# provides automatically implemented properties. They are a short version of the code above; however, the backing variable is hidden and not accessible (it is accessible from within the class in VB, however).

public string Id { get; set; }

您可以简单地使用它们像这样(就好像你在访问一个字段)

You can simply use them like this (as if you were accessing a field)

var obj = new MyClass();
obj.Id = "xy";       // Calls the setter with "xy" assigned to the value parameter.
string id = obj.Id;  // Calls the getter.


使用属性,你会在列表中这样的搜索项目


Using properties, you would search for items in the list like this

MyClass result = list.Find(x => x.Id == "xy"); 


您还可以,如果你需要一个只读属性中使用自动实现的属性:


You can also use automatically implemented properties if you need a read-only property:

public string Id { get; private set; }

这使您可以设置编号类中而不是从外面。如果您需要将其设置在派生类中,以及还可以保护二传手

This enables you to set the Id within the class but not from outside. If you need to set it in derived classes as well you can also protect the setter

public string Id { get; protected set; }


最后,你可以声明属性虚拟并覆盖它们在派生类,允许你提供getter和setter方法​​不同的实现;就像普通的虚方法。


And finally, you can declare properties as virtual and override them in deriving classes, allowing you to provide different implementations for getters and setters; just as for ordinary virtual methods.

由于C#6.0(Visual Studio的2015年,罗斯林),你可以只写消气自动性能与内嵌初始化

Since C# 6.0 (Visual Studio 2015, Roslyn) you can write getter-only auto-properties with an inline initializer

public string Id { get; } = "A07"; // Evaluated once when object is initialized.

您也可以在构造函数中仅初始化消气属性。

You can also initialize getter-only properties within the constructor.

开始用C#6.0,你也可以写属性前pression健全成员

Beginning with C# 6.0 you can also write properties as expression-bodied members

public DateTime Yesterday => DateTime.Date.AddDays(-1); // Evaluated at each call.
// Instead of
public DateTime Yesterday { get { return DateTime.Date.AddDays(-1); } }

请参阅: .NET编译器平台(罗斯林)结果
          新的语言功能在C#6

这篇关于我怎样才能找到一个List℃的特定元素; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆