前言:漫长的国庆长假终于快结束了,我们该工作的工作了,该学习的学习了,该干什么的也都开始了,这篇博客我主要说一下C#中集合的使用,也就是ArrayList集合和Hashtable字典集合的使用,我在这里说的主要还是简单的使用,使学习的能够很快的学习集合然后自己研究集合的一些高级用法,在最后还列举出了一些常用的小案例。
- ArrayList
(1) 命名空间 System.Collection
(2)创建对象
1)增
->Add
->AddRange 任何一个集合对象
int[] num = { 1, 2, 3, 4, 5, 6 };
ArrayList arr = new ArrayList();
//ICollection一个对象里面包含很多其他对象
arr.AddRange(num);
->Insert
2)删
->Remove
->RemoveAt
->Clear
3)改
->像数组一样通过索引来修改
4)查
->Contains
->IndexOf
- 自定义集合
(1) 集合是Object类型,处理数据的时候会需要强转等操作
(2)举例说明:
1)新建一个Person类
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public char Gender { get; set; }
public Person(string n, int a, char g)
{
Name = n;
Age = a;
Gender = g;
}
public void SayHello()
{
Console.WriteLine("我叫{0}", Name);
}
}
2)新建一个PeresonCollection类
class PersonCollection
{
ArrayList arr = new ArrayList();
public void Add(Person p)
{
arr.Add(p);
}
public void AddRange(params Person[] ps)
{
arr.AddRange(ps);
}
public Person this[int index]
{
get { return (Person)arr[index]; }
}
public int Count
{
get { return arr.Count; }
}
}
3)在main方法中实现
static void Main(string[] args)
{
PersonCollection ps = new PersonCollection();
ps.Add(new Person("韩迎龙", 23, '男'));
ps.Add(new Person("韩全龙", 16, '男'));
ps.Add(new Person("泛亚内", 16, '男'));
ps.Add(new Person("野田", 16, '男'));
for (int i = 0; i < ps.Count; i++)
{
ps[i].SayHello();
}
Console.ReadKey();
}
(3)定义索引
1)就是一个通过传进来的参数访问内部数据的一个属性(带有参数的属性)
2)public 返回类型 this[索引参数类型 参数名]
{
get{return 通过参数访问的数据;}
set{通过参数处理要修改的数据=value}
}
案例说明:
class Arr
{
int[] nums = { 10, 23, 34, 45, 56 };
public int this[int index]
{
get {
if (index > nums.Length - 1)
{
throw new Exception("索引超出异常");
}
return nums[index];
}
set { nums[index] = value; }
}
public int Count
{
get { return nums.Length; }
}
}
class IndexString
{
int num = 10;
string str = "字符串韩迎龙";
char ch = 'c';
public int this[int index]
{
get { return num; }
}
public string this[string str]
{
get { return this.str; }
}
public char this[char c]
{
get { return ch; }
}
public string this[int n, char c, string s]
{
get { return num + str + ch; }
}
}
class Program
{
static void Main(string[] args)
{
IndexString IS=new IndexString();
Console.WriteLine(IS[' ']);
Console.WriteLine(IS[0, ' ', " "]);
Console.ReadKey();
}
}
3)模拟实现集合
class MyCllection
{
ArrayList arrInt = new ArrayList();
ArrayList arrStr = new ArrayList();
ArrayList obj=new ArrayList();
public void Add(string key, object obj)
{
int index = arrStr.Add(key);
arrInt.Add(index);
this.obj.Add(obj);
}
public object this[int index]
{
get { return obj[index]; }
}
public object this[string key]
{
get { return obj[arrStr.IndexOf(key)]; }
}
}
static void Main(string[] args)
{
MyCllection mc = new MyCllection();
mc.Add("HYL", "韩迎龙");
mc.Add("HYL", "韩全龙");
Console.WriteLine(mc[0]);
Console.WriteLine(mc["HYL"]);
Console.ReadKey();
}
- foreach语法
(1) 执行
1)可以只遍历一部分吗? 不能
2)可以倒着遍历吗? 不能
3)举例说明:
int[] nums = { 1, 4, 6, 87, 87, 3, 23, 45 };
foreach (int item in nums)
{
Console.WriteLine(item);
}
Console.ReadKey();
(2)数据源应该是一个可枚举的类型(IEnumerable)
(3)foreach循环的步骤
1)foreach循环首先到数据集中调用GetEnumerator方法得到枚举器对象
2)到in关键字,调用MoveNext()方法
3)这个方法返回bool值,将枚举器移到数据集的下一个数据上,并检查是否有数据,如果有,返回true,否则返回false
4)到临时变量(迭代变量),开始调用枚举器的Current属性,将当前的数据取出来,然后赋值给临时变量
5)就可以使用了
- 嵌套类
(1) 有时候在某个类型中需要一个特定的类型处理数据,而这个类型只在当前对象类中可使用
(2)可以将这个类定义在一个类的里面,此时称为嵌套类
(3)嵌套类的访问修饰符和一般成员一样
(4)一般是为了考虑安全问题
- yield迭代器
(1) 一般的语法:yield return 数据
(2)foreach在执行开始访问数据集的时候,依旧是调用IEnumerable中的GetEnumerator方法
(3)这个Getenumerator方法可以使用这个迭代器返回数据
(4)在方法中写上循环,循环体上放上yield,那么循环不会立即结束
(5)每当执行到yield的时候就会停在那里,跳到foreach中
(6)foreach执行到in的时候,又会跳到上次停留的yield那里,继续向下执行
(7)直到下一个yield,再次调用foreach
public IEnumerator GetEnumerator()
{
for(int i=0;i<arr.Count;i++)
{
yield return arr[i];
}
}
- Hashtable 字典集合
(1) 举例说明:
Hashtable has = new Hashtable();
has.Add("BigBoss", "韩迎龙");
has.Add("sdd", "的事实");
has.Add("dfd", "的速度");
Console.WriteLine(has["BigBoss"]);
(2)Hashtable的使用,实现一个电子词典
class Program
{
static void Main(string[] args)
{
string[] words = File.ReadAllLines("英汉词典TXT格式.txt", Encoding.Default);
Hashtable has = new Hashtable();
for (int i = 0; i < words.Length; i++)
{
string[] temp = words[i].Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length == 1)
{
continue;
}
string key = temp[0];
string value = string.Join(" ", temp, 1, temp.Length - 1);
if (!has.ContainsKey(key))
{
has.Add(key, value);
}
else
{
has[key] = string.Format("{0}\n{1}", has[i], value);
}
}
//可以查看了
while (true)
{
Console.Write("请输入单词?");
string str=Console.ReadLine();
if (has.ContainsKey(str))
{
Console.WriteLine(has[str]);
}
else
{
Console.WriteLine("很抱歉,你说查询的词汇没有找到");
}
}
Console.ReadKey();
}
}
static void Main(string[] args)
{
string[] words = File.ReadAllLines("英汉词典TXT格式.txt", Encoding.Default);
Hashtable has = new Hashtable();
for (int i = 0; i < words.Length; i++)
{
string[] temp = words[i].Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);
string key = temp[0];
string value = temp[1];
if (temp.Length != 2)
{
continue;
}
if (!has.ContainsKey(key))
{
has.Add(key, value);
}
else
{
has[key] = string.Format("{0}\n{1}", has[i], value);
}
}
//可以查看了
while (true)
{
Console.Write("请输入单词?");
string str=Console.ReadLine();
if (has.ContainsKey(str))
{
Console.WriteLine(has[str]);
}
else
{
Console.WriteLine("很抱歉,你说查询的词汇没有找到");
}
}
Console.ReadKey();
}
}
(3)如何遍历?
static void Main(string[] args)
{
Hashtable has = new Hashtable();
has.Add("1", "韩迎龙");
has.Add("2", "得到");
//仅仅遍历key
foreach (string item in has.Keys)
{
Console.WriteLine(item);
}
//遍历value
foreach (string item in has.Values)
{
Console.WriteLine(item);
}
//遍历整个集合
foreach (DictionaryEntry item in has)
{
Console.WriteLine("{0}是{1}", item.Key, item.Value); ;
}
Console.ReadKey();
}
(4)var 类型推断
1)var val=数据;
2)赋什么数据就是什么类型
3)类型确定以后就不能变了
4)必须保证类型赋值时时确定的
5)何时使用类型推断
->匿名类型
static void Main(string[] args)
{
var person = new { Name = "张三", Age = 23, Gender = '男' };
Console.WriteLine(person.Name);
Console.WriteLine(person.Age);
Console.WriteLine(person.Gender);
Console.ReadKey();
}
->Linq查询
static void Main(string[] args)
{
int[] nums = { 132, 454, 667, 343, 65, 76, 74 };
var query = from n in nums
where n > 100 && n < 500
select n;
foreach (var item in query)
{
Console.WriteLine(item);
}
Console.ReadKey();
}