自适应网站运动div如何设置的,附近电脑培训学校,网站开发最严重的问题,用wordpress还是discuz文章目录 数组单维数组多维数组交错数组 索引器类上的索引器方法1方法2 接口中的索引器 数组
数组具有以下属性#xff1a;
数组可以是一维、多维或交错的。创建数组实例时#xff0c;将建立纬度数量和每个纬度的长度。 这些值在实例的生存期内无法更改。数值数组元素的默认… 文章目录 数组单维数组多维数组交错数组 索引器类上的索引器方法1方法2 接口中的索引器 数组
数组具有以下属性
数组可以是一维、多维或交错的。创建数组实例时将建立纬度数量和每个纬度的长度。 这些值在实例的生存期内无法更改。数值数组元素的默认值设置为0而引用元素设置为 null。交错数组是数组的数组因此其元素为引用类型且被初始化为 null。数组从零开始编制索引包含 n 元素的数组从 0 索引到 n-1。数组元素可以是任何类型其中包括数组类型。数组类型是从抽象的基类型 Array 派生的引用类型。 所有数组都会实现 IList 和 IEnumerable。可以使用 foreach 语句循环访问数组。 单维数组还实现了 IList 和 IEnumerable。
数组可以静态定义也可以隐式定义
单维数组
int[] array new int[5];int[] array1 new int[] { 1, 3, 5, 7, 9 }; // 定长为5不是动态的int[] array2 { 1, 3, 5, 7, 9 }; // 定长
string[] weekDays2 { Sun, Mon, Tue, Wed, Thu, Fri, Sat };多维数组
与其他语言定义数组不同定义多维数组使用的是[,]其中每多一个问号多一个维度。在C#中[][]并不是定义的多维数组而是交错数组也就是数组中的数组。
int[,] array new int[4, 2]; //4 * 3 的二维数组
int[,,] array1 new int[4, 2, 3]; // 4 * 2 * 3 的三维数组显式定义多维数组的时候必须全部定义完。
int[,] array2Da new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
string[,] array2Db new string[3, 2] { { one, two }, { three, four },{ five, six } };隐式定义
int[,,] array3D new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },{ { 7, 8, 9 }, { 10, 11, 12 } } };不指定级别也能定义
int[,] array4 { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };交错数组
交错数组被称为数组中的数组其内部往往定义了多个数组。
下列定义了一个交错数组第一个[]定义了内部有三个数组第二个[]定义了内部数组的维度这里只有一维
int[][] jaggedArray new int[3][];
// 初始化,数组只需维度相同元素数不定
jaggedArray[0] new int[5];
jaggedArray[1] new int[4];
jaggedArray[2] new int[2];
// 上述定义和下面语句相同
jaggedArray[0] new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] new int[] { 0, 2, 4, 6 };
jaggedArray[2] new int[] { 11, 22 };还可以在声明时初始化
// 交错数组new声明的第一个[]不定义数量
// 但是new声明的第二个[]必须定义维度数量
int[][] jaggedArray2 new int[][]
{
new int[] { 1, 3, 5, 7, 9 },
new int[] { 0, 2, 4, 6 },
new int[] { 11, 22 }
};
// 定义的二维交错数组
int[][,] jaggedArray3 new int[][,]
{
new int[,] { { 1,2 }, { 3,3 }}
;也可以不用new来声明
// 不使用new声明定义时也需要数组同维度
int[][] jaggedArray4
{new int[] { 1, 3, 5, 7, 9 },new int[] { 0, 2, 4, 6 },new int[] { 11, 22 }
};指定元素数和数组维度定义多维数组的例子
int[][,] jaggedArray5 new int[3][,]
{new int[,] { {1,3}, {5,7} },new int[,] { {0,2}, {4,6}, {8,10} },new int[,] { {11,22}, {99,88}, {0,9} }
};索引器
使用索引器其目的是为了访问类中非public的数组类。并且可以自定义访问索引的get方法和set方法。索引器可以被定义在类和接口上。
索引器及其参数的类型必须至少具有和索引器相同的可访问性。索引器值不能按引用作为 ref 或 out 参数传递。
类上的索引器
方法1
class Parent
{string[] s { 123, 222 };public string this[int index]{get{return s[index];}set{s[index] value;}}
}
void Start()
{Parent p new Parent();Debug.Log(p[1]); // 222p[1] 555; // 输入值默认valueDebug.Log(p[1]); // 555
}使用索引器用于访问类中的元素一般是数组元素通常通过index访问但是也可以用于其他需要使用序号的情况例如使用序号来访问int型中的第n个数字当然也能访问枚举类型。。
上述的索引器可以用设置get和set方法
class Parent
{string[] s { 123, 222 };public string this[int index]{get s[index];set s[index] value;}
}方法2
在下例中使用索引值来访问索引下面设置的索引器是只读的实现了通过索引值来访问索引号的方法。
class DayCollection
{string[] days { Sun, Mon, Tues, Wed, Thurs, Fri, Sat };public int this[string day] FindDayIndex(day);private int FindDayIndex(string day){for (int j 0; j days.Length; j){if (days[j] day){return j;}}throw new ArgumentOutOfRangeException(nameof(day),$Day {day} is not supported.\nDay input must be in the form \Sun\, \Mon\, etc);}
}
void Start()
{DayCollection Days new DayCollection();Debug.Log(Days[Sat]);
}使用访问器get和set方法来定义索引器以及其他变量的可读性和可写性。 接口中的索引器
// Indexer on an interface:
public interface IIndexInterface
{// Indexer declaration:int this[int index]{get;set;}
}// Implementing the interface.
class IndexerClass : IIndexInterface
{private int[] arr new int[100];public int this[int index] // indexer declaration{// The arr object will throw IndexOutOfRange exception.get arr[index];set arr[index] value;}
}根据上述的例子接口IIndexInterface中定义了一个索引器但是接口本身并无int数组。并且接口中定义了get和set权限则在继承类中必须实现索引器的get和set。因此索引器的读写权限在接口中已经定义了。
IndexerClass继承了该接口并在接口中定义了索引器的读写访问器。
当继承了多个同名索引器的时候当然也需要使用完全限定名来定义
string IIndexInterface.this[int index]
{
}