namespace ConsoleApplication1
{
class Program
{
public Program() { }
public void swap<T>(ref T x, ref T y)
{
T m;
m = x;
x = y;
y = m;
}
/*泛型方法*/
static void Main(string[] args)
{
Program pro = new Program();
string str1 = "zadk";
string str2 = "zbc";
pro.swap(ref str1,ref str2);
Console.WriteLine("{0},{1}",str1,str2);
Console.Read();
}
}
}
集合类List的用法
static void Main(string[] args) {
List<string> item = new List<string>();
item.Add("清华同方");
item.Add("IBM");
item.Insert(0, "lenov");
string[] title = { "Tecent", "华硕" };
item.AddRange(title);
Console.WriteLine("起始列表项:");
foreach (string str in item) { Console.Write("{0}\t",str); }
Console.WriteLine("list中共有{0}项",item.Count);
Console.WriteLine("查找指定元素huashuo:{0}", item.IndexOf("huashuo"));
Console.WriteLine("查找指定元素Tecent:{0}", item.IndexOf("Tecent"));
Console.WriteLine("删除指定元素'清华同方后'元素列表为:");
item.Remove("清华同方"); foreach (string str in item)
{ Console.Write("{0}\t", str); }
item.Clear();
foreach (string str in item)
{ Console.Write("{0}\t", str); }
Console.Read();
}
综合用
namespace ConsoleApplication1
{
publicclassContact {
public Contact() { }
publicstring name { get; set; }
publicstring phone { get; set; }
}
classProgram
{
public Program() { }
staticvoid Main(string[] args)
{
string[] sname = { "Jerry","Mary","Angela","Liminhao"};
string[] sphone = { "11","22","33","44"};
List<Contact> person = newList<Contact>();
Contact con = null;
String search = "";
for (int i = 0; i < sname.Length; i++)
{
con = newContact();
con.name = sname[i];
con.phone = sphone[i];
person.Add(con);
}
Console.WriteLine("=======================================");
Console.WriteLine("******按姓名查询号码*************");
do
{
Console.WriteLine("请输入要查询人电话,以q键结束");
search = Console.ReadLine();
foreach (Contact item in person)
{
if (item.phone == search)
{
Console.WriteLine("电话是{0}的人姓名是{1}", search, item.name);
}
}
} while (search != "q");
Console.Read();
}
}
}