泛型集合list<T>
namespace _03_06泛型集合ListT
{
class Program
{
static void Main(string[] args)
{
//List<T>
//1.定义自己的第一个泛型集合
List<string> list = new List<string>();
list.Add("彪哥");
list.Add("敏姐");
list.Add("小吴老师");
//泛型集合,是将报错提前到了编译时
//遍历一道
foreach (string item in list)
{
Console.WriteLine(item);
}
//敏姐 移除
list.Remove("敏姐");
Console.WriteLine("====================");
foreach (string item in list)
{
Console.WriteLine(item);
}
}
}
泛型集合Dictionary<K,V>
namespace _03_06泛型集合ListT
{
class Program
{
static void Main(string[] args)
{
//List<T>
//1.定义自己的第一个泛型集合
List<string> list = new List<string>();
list.Add("彪哥");
list.Add("敏姐");
list.Add("小吴老师");
//泛型集合,是将报错提前到了编译时
//遍历一道
foreach (string item in list)
{
Console.WriteLine(item);
}
//敏姐 移除
list.Remove("敏姐");
Console.WriteLine("====================");
foreach (string item in list)
{
Console.WriteLine(item);
}
}
}
}
namespace _03_07DictionayKV
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, Book> dic = new Dictionary<string, Book>();
Book book1=new Book(){BookId="001",BookName="钢铁是怎样炼成的---尼古拉·奥斯特洛夫斯基",BookPrice=78};
Book book2=new Book(){BookId="002",BookName="平凡的世界-------路遥",BookPrice=78};
Book book3=new Book(){BookId="003",BookName="百年孤独-------XXX",BookPrice=98};
dic.Add(book1.BookId,book1);
dic.Add(book2.BookId, book2);
dic.Add(book3.BookId, book3);
//三种 第一种 根据key获取value
foreach (string item in dic.Keys)
{
Console.WriteLine("key是"+item);
//获取value
Book book= dic[item];
Console.WriteLine(book.BookName);
}
//第二种 直接获取value
foreach (Book item in dic.Values)
{
Console.WriteLine("图书的名称"+item.BookName);
}
//第三种 key 和 value
foreach (KeyValuePair<string,Book> item in dic)
{
Console.WriteLine(item.Key);
//任何对象都会自动调用他的ToString()
Console.WriteLine(item.Value.BookName); //打印出什么
}
}
}
}