master
bicijinlian 5 years ago
parent 81efe9ee26
commit e25925f12c

@ -10,7 +10,9 @@ using Xunit;
namespace LinqStudy.Test.LinqToObject
{
/// <summary>
/// 基本项测试
/// 查询运算符:本质定义在下面程序集中的扩展方法,少量静态方法。
/// System.Linq.Enumerable
/// System.Linq.Queryable
/// </summary>
public class BaseTest
{

@ -0,0 +1,22 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
namespace LinqStudy.Test.LinqToObject
{
/// <summary>
/// 转换运算符
/// </summary>
public class ConvertTest
{
[Fact]
public void Test()
{
var cc= Enumerable.Empty<string>();
}
}
}

@ -1,4 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using FluentAssertions;
namespace LinqStudy.Test.LinqToObject
{
@ -7,5 +13,32 @@ namespace LinqStudy.Test.LinqToObject
/// </summary>
public class CreateTest
{
/// <summary>
/// Empty 返回指定类型的空集,没有任何元素的集合。
/// Enumerable的静态方法常用来做初始种子集合。
/// </summary>
/// <remarks>
/// Enumerable
/// </remarks>
[Fact]
public void Empty_string_Test()
{
var ini= Enumerable.Empty<string>();
Assert.IsType<string[]>(ini);
Assert.Empty(ini);
}
/// <summary>
/// 注意返回值为Enumerable<T>,而不是List<T>
/// </summary>
[Fact]
public void Empty_Class_Test()
{
var ini = Enumerable.Empty<Person>();
Assert.IsType<Person[]>(ini);
Assert.Empty(ini);
}
}
}

@ -0,0 +1,81 @@
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;
}
}
}
}

@ -0,0 +1,108 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace LinqStudy.IEnumerableDemo
{
/// <summary>
/// 普通学生类
/// </summary>
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
/// <summary>
/// 实现可枚举接口
/// </summary>
public class Students : IEnumerable
{
private Student[] arrStudent;
public Students(Student[] collection)
{
arrStudent = collection;
}
public Student this[int index]
{
get { return arrStudent[index]; }
}
public int Count
{
get { return arrStudent.Length; }
}
/// <summary>
/// 实现接口
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return new StudentIterator(this);
}
}
/// <summary>
/// 实现枚举器接口
/// </summary>
public class StudentIterator : IEnumerator
{
private Students arrStudents;
private int idx;
internal StudentIterator(Students collection)
{
this.arrStudents = collection;
idx = -1;
}
object IEnumerator.Current
{
get { return arrStudents[idx]; }
}
public bool MoveNext()
{
idx++;
return idx < arrStudents.Count;
}
public void Reset()
{
idx = -1;
}
public void Dispose() { }
}
//使用
public class UseDemo
{
public void Test()
{
Students arrStudent = new Students
(
new Student[]
{
new Student() { Name = "张三", Age = 23 },
new Student() { Name = "李四", Age = 24 },
new Student() { Name = "王五", Age = 45 },
new Student() { Name = "DDD", Age = 21 },
new Student() { Name = "BBB", Age = 14 },
new Student() { Name = "CCC", Age = 3 },
new Student() { Name = "AAA", Age = 22 },
new Student() { Name = "BBB", Age = 55 },
new Student() { Name = "CCC", Age = 43 },
}
);
foreach (Student obj in arrStudent)
{
Console.WriteLine("姓名:" + obj.Name + ",年龄:" + obj.Age);
}
}
}
}
Loading…
Cancel
Save