You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
1.7 KiB
C#
82 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
using Xunit;
|
|
|
|
namespace LinqStudy.Test.Other
|
|
{
|
|
/// <summary>
|
|
/// 索引器测试
|
|
/// </summary>
|
|
public class IndexersTest
|
|
{
|
|
[Fact]
|
|
public void Test()
|
|
{
|
|
PersonIndexers indexer = new PersonIndexers( PersonManager.GetPersons());
|
|
|
|
Assert.Equal(1, indexer[0].Id);
|
|
}
|
|
}
|
|
|
|
public class PersonIndexers
|
|
{
|
|
private List<Person> Persons=new List<Person>();
|
|
|
|
public PersonIndexers(List<Person> people)
|
|
{
|
|
Persons = people;
|
|
}
|
|
|
|
public Person this[int index]
|
|
{
|
|
get
|
|
{
|
|
if (index >= 0 && index < Persons.Count)
|
|
{
|
|
return Persons[index];
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
set {
|
|
if (index >= 0 && index < Persons.Count)
|
|
{
|
|
Persons[index] = value;
|
|
}
|
|
else
|
|
{
|
|
throw new IndexOutOfRangeException();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public class EmployeeIndexers
|
|
{
|
|
private List<Employee> Employees;
|
|
|
|
public EmployeeIndexers(List<Employee> _employees)
|
|
{
|
|
Employees = _employees;
|
|
}
|
|
|
|
public Employee this[string name]
|
|
{
|
|
get
|
|
{
|
|
return Employees.Find(q => q.Name== name);
|
|
}
|
|
set
|
|
{
|
|
var idx= Employees.FindIndex(q => q.Id == value.Id);
|
|
Employees[idx] = value;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|