using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
namespace LinqStudy.Test.Other
{
///
/// 索引器测试
///
public class IndexersTest
{
[Fact]
public void Test()
{
PersonIndexers indexer = new PersonIndexers( PersonManager.GetPersons());
Assert.Equal(1, indexer[0].Id);
}
}
public class PersonIndexers
{
private List Persons=new List();
public PersonIndexers(List 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 Employees;
public EmployeeIndexers(List _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;
}
}
}
}