这里将记载一些c#的知识点补充
这里还是一些c#的基础知识补偿,包括了一些接口方面的细节啦,泛型的介绍,还有关于泛型如何使用去创造list集合类,还有索引器的运用,当然这里索引器的运用讲的不够完善,我会在下一篇中进行完善,下一篇还会同步更新一部分练习题和解题方法,希望能帮助到各位
1 接口中只能包含抽象方法,而抽象基类可以包含非抽象方法
.2.接口中不能有虚方法,一个类可以实现多个接口,且接口不能被实例化
3.当类继承多个接口时,接口用逗号进行分隔
4.接口不能被实例化
5.继承自抽象类的子类必须实例化父类的所有抽象方法
开始泛型的学习
6,当我们有多个类型相同的数据时,我们常常通过数组进行存储,但是这样有个问题,就是数组的大小是固定的,这时候我们可以引入泛型,比如集合类的列表list,可以存储游戏类的数据,类似于分数
7.list的创建方式 1.List<int>scorelist=new List<int>();
2.var scorelist=newList<int>();
Varscorlist=new List<int>(){1,2,3}表示创建了一个存储1,2,3的列表
Scorelist.Add(12)即表示向列表中插入数据,,
而我们访问数据时候,和访问数组很像,也是通过索引来输出输出
Console.WriteLine(scroelist[0]);
8.列表可以存储任何类型的数据,在创建时首先你要确定你存储的是什么类型的
列表的容量,dang加第一个数值时,容量会变成四,然后每次超出容量时,容量会翻倍增加,而获取数值,还是按照count获取
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
var scorelist = new List<int> { };
scorelist.Add(1);
scorelist.Add(2);
for(int i = 0; i < 20; i++)
{
scorelist.Add(10);
Console.WriteLine("capicty:" + scorelist.Capacity + "count:" + scorelist.Count);
}
Console.ReadKey();
}
}
}
这是结果
7.泛型类
定义一个泛型类是指类中的某些字段的类型是不确定的,这些类型可以在类的构造时候被确定下来
namespace 泛型类
{
class ClassA<T>
{
private T a;
private T b;
public ClassA(T a,T b)
{
this.a = a;this.b = b;
}
public string getsum()
{
return a + "" + b;
}
}
}
然后在主类中调用
namespace 泛型类
{
class Program
{
static void Main(string[] args)
{
var o1 = new ClassA<int>(12, 35);
o1.getsum();
Console.ReadKey();
}
}
}
8.泛型方法
泛型方法指的是我们可以定义一些方法,但是不给出参数类型,我们可以在调用它的时候再定义参数类型
namespace 泛型类
{
class Program
{
public static string getSum<T>(T a,T b)
{
return a + "" + b;
}
static void Main(string[] args)
{
Console.WriteLine(getSum(23, 45));
Console.WriteLine(getSum(23.2, 45.6));
Console.WriteLine(getSum("hwied", "uwedi"));
Console.ReadKey();
}
}
}
9.创建自己的list来存放数据
namespace 自己的泛型集合
{
class MyList<T>
{
private T[] array;
private int count;
public MyList(int size)
{
if (size > 0)
{
array = new T[size];
}
}
public MyList()
{
array = new T[0];
}
public int Capacity
{
get { return array.Length; }
}
public int Count
{
get
{
return count;
}
}
public void Add(T item)
{
if (Capacity == count)
{
if (count == 0)
{
array = new T[4];//当一个数组为0的时候,创建一个长度为4的数组,如果一样大,说明数组容量不够
}
else
{
var newArray = new T[Capacity * 2];//此时我们将数组变成原来长度的两倍
Array.Copy(array, newArray, count);//把旧数组的元素赋值到新数组
array = newArray;
}
}
array[Count] = item;
count++;
}
}
}
9.索引器,通过index的方式访问数据就是索引器,下面的代码就展示了通过索引器寻找集合中的数据
public T GetItem(int index)
{
if (index >= 0 && index <= count - 1)
{
return array[index];
}
else
{
throw new Exception("索引超出范围");
}
}
namespace 自己的泛型集合
{
class Program
{
static void Main(string[] args)
{
MyList<int> mylist = new MyList<int>();
mylist.Add(234);
mylist.Add(2);
mylist.Add(24);
mylist.Add(34);
mylist.Add(274);
for (int i = 0; i < mylist.Count; i++)
{
Console.WriteLine(mylist.GetItem(i));
}
Console.ReadKey();
}
}
}
当然还有一种方法来获取索引
通过get块和set块来获取索引值和设置索引值,查找T具体方法,可以按f12进行查询
然后想要获取值就在main函数里进行调用了
Console.WriteLine(mylist[i]);这个就是通过Get块取值
Mylist[0]=100,这就是通过set块设置值
public T this[int index]
{
get
{
return GetItem(index);
}
set
{
if (index >= 0 && index <= count - 1)
{
array[index] = value;
}
else
{
throw new Exception("索引超出范围");
}
}
}