1.引入命名空间System.Collections,使用.NET提供泛型类Stack<T>,实现字符串或数字的反序
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
namespace Program0
{
class Program
{
static void Main(string[] args)
{
Stack<String> t = new Stack<string>();
string str = "abcdefg";
char[] s = str.ToCharArray();
for (int i = 0; i < str.Length; i++)
{
t.Push(s[i].ToString());
}
while (t.Count > 0)
{
Console.Write(t.Pop());
}
Console.ReadKey();
}
}
}
2.引入命名空间System.Collections,使用.NET提供泛型类Queue<T>,实现任意数值型数据的入队、出队操作。并将出队数据求和。
using System;
using System.Collections;
using System.Collections.Generic;
namespace Program0
{
class Program
{
static void Main(string[] args)
{
Queue<int> t = new Queue<int>();
int[] a = { 1, 2, 4, 6, 8, 9, 10 };
int sum = 0;
for (int i=0;i<a.Length;i++)
{
t.Enqueue(a[i]);
}
while (t.Count>0)
{
sum += t.Peek();
Console.WriteLine("出队:{0}", t.Dequeue());
}
Console.WriteLine("出队数据之和为:{0}", sum);
Console.ReadKey();
}
}
}
3.编写一个使用匿名方法的委托,匿名方法实现计算整数型数组各元素之和的功能。
static void Main(string[] args)
{
int []a={1,2,3,4,5,6,7,8,9};
MyDelegate s = delegate(int []b)
{
int sum = 0;
for (int i = 0; i < a.Length; i++)
{
sum += a[i];
}
Console.WriteLine("元素之和为{0}", sum);
};
s(a);
Console.ReadKey();
}
}
}