迭代器概述
实现迭代器最常用的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace _18._2创建迭代器最常用的方法
{
class Program
{
public static IEnumerable SimpleDemmo()
{
yield return "string1";
yield return "string2";
yield return "string3";
yield break;//迭代终止
yield return "string4";
yield return "string5";
}
static void Main(string[] args)
{
foreach(string item in SimpleDemmo())
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
结果:
string1
string2
string3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace _18._2创建迭代器最常用的方法
{
class Program
{
public class Months:IEnumerable//此处不是静态的,所以在主函数中使用时得实例化
{
string[] month = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Nov", "Dec" };
public IEnumerator GetEnumerator()
{
for (int i = 0; i < month.Length; i++)
{
yield return month[i];
; }
}
}
static void Main(string[] args)
{
Months mon = new Months();//实例化
foreach(string m in mon)
{
Console.WriteLine(m);
}
Console.ReadKey();
}
}
}
结果:
Jan
Feb
Mar
Apr
May
June
July
Aug
Sep
Nov
Dec
分部类概述及其使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18._3分部类概述及其使用
{
partial class arithmetic//阶乘
{
public int factorial(int num)
{
int factorial = 1;
for (int i = num; i > 0; i--)
{
factorial *= i;
}
return factorial;
}
}
partial class arithmetic//平方
{
public int squate(int num)
{
return num * num;
}
}
class Program
{
static void Main(string[] args)
{
arithmetic a = new arithmetic();
int num = 5;
Console.WriteLine("调用分部类的第一部分:{0}", a.factorial(num));
Console.WriteLine("调用分部类的第二部分:{0}", a.squate(num));
Console.ReadKey();
}
}
}
结果:
调用分部类的第一部分:120
调用分部类的第二部分:25
索引器概述及声明
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18._4索引器概述及声明
{
public class Clerk
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private char gender;
public char Gender
{
get
{
if (gender != '男' && gender != '女') gender = '男';
return gender;
}
set { gender = value; }
}
private int[] myint = new int[10];
//public int this[int index]//声明索引器
//{
// get { return myint[index]; }
// set { myint[index] = value; }
//}
//public virtual int this[int index]//虚拟索引器
//{
// get { return myint[index]; }
//}
public extern int this[int index]//外部索引器
{
get;set;
}
}
abstract class indexEaxmple//抽象索引器,在抽象类中声明
{
public abstract int this[int index]
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
}
}
}
索引器在类中使用
索引器最大的用处就是在类中使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18._5索引器在类中的使用
{
class Program
{
public class indexText//访问类实例
{
private int[] array = new int[10];
public int this[int index]
{
get
{
if (index < 0 || index >= 10) return 0;
else return array[index];
}
set
{
if (index >= 0 && index < 10) array[index] = value;
}
}
}
public class weekIndex//访问类成员
{
string[] Week = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
private int getDay(string weekText)
{
int i = 0;
foreach(string day in Week)
{
if (day == weekText) return i;//从Week[0]开始比较,当相同时返回比较的次数几位Week的索引值
i++;
}
return -1;
}
public int this [string week]
{
get { return getDay(week); }
}
}
static void Main(string[] args)
{
Console.WriteLine("访问类实例的结果:");
indexText Arr = new indexText();
Arr[-5] = 5;
Arr[0] = 15;
Arr[1] = 30;
Arr[2] = 60;
Arr[11] = 65;
Console.WriteLine("Arr[-5]={0}", Arr[-5]);
Console.WriteLine("Arr[0]={0}", Arr[0]);
Console.WriteLine("Arr[1]={0}", Arr[1]);
Console.WriteLine("Arr[2]={0}", Arr[2]);
Console.WriteLine("Arr[11]={0}", Arr[11]);
Console.WriteLine("访问类成员结果:");
weekIndex we = new weekIndex();
Console.WriteLine(we["星期三"]);
Console.WriteLine(we["星期四"]);
Console.WriteLine(we["星期日"]);
Console.WriteLine(we["星期八"]);
Console.ReadKey();
}
}
}
结果:
访问类实例的结果:
Arr[-5]=0
Arr[0]=15
Arr[1]=30
Arr[2]=60
Arr[11]=0
访问类成员结果:
2
3
6
-1
索引器在接口中使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18._6索引器在接口中的使用
{
public interface ItextIndex
{
int this[int index]//索引器在接口中声明
{
get;
set;
}
}
class indexText:ItextIndex
{
private int[] array = new int[10];
public int this[int index]
{
get { if (index < 0 || index >= 10) return 0;
else return array[index];
}
set { if (index > 0 && index < 10) array[index] = value; }
}
}
class Program
{
static void Main(string[] args)
{
indexText Arr = new indexText();
Arr[-1] = 5;
Arr[4] = 10;
Arr[9] = 20;
Arr[14] = 30;
for(int i=-1;i<15;i=i+5)
{
Console.WriteLine("Arr[{0}]={1}", i, Arr[i]);
}
Console.ReadKey();
}
}
}
结果:
Arr[-1]=0
Arr[4]=10
Arr[9]=20
Arr[14]=0
本章小结及任务实施
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18._7本章小结及任务实施
{
public class QQStateIndex
{
string[] qqState = { "在线", "离线", "忙碌", "Q我", "隐身" };
private int getState(string stateText)//方法
{
int i = 0;
foreach(string stateString in qqState)
{
if (stateString == stateText) return i;
i++;
}
return -1;
}
public int this[string stateSting]//索引器
{
get { return getState(stateSting); }
}
}
class Program
{
static void Main(string[] args)
{
QQStateIndex qs = new QQStateIndex();
Console.WriteLine(qs["在线"]);
Console.WriteLine(qs["忙碌"]);
Console.WriteLine(qs["断线"]);
Console.ReadKey();
}
}
}
结果:
0
2
-1