110,538
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
string Tem_str = "";
string str = "abcdefg"; //对str字符串逆序输出
foreach (object i in Transpose(str))
{
Tem_str += i.ToString();
}
Console.WriteLine(Tem_str);
Console.ReadKey();
}
public static IEnumerable<object> Transpose(string n)
{
for (int i = n.Length - 1; i >= 0; i++)
{
yield return (object)n[i];
}
}
// yield-example.cs
using System;
using System.Collections;
public class List
{
public static IEnumerable Power(int number, int exponent)
{
int counter = 0;
int result = 1;
while (counter++ < exponent)
{
result = result * number;
yield return result;
}
}
static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2, 8))
{
Console.Write("{0} ", i);
}
}
}
//输出:2 4 8 16 32 64 128 256