文章目录
- 可空类型
- NULL合并运算符(??)
- 数组
- 数组声明
- 数组初始化
- 数组赋值
- 数组访问
- 多维数组
- 交错数组
- 数组类
- 数组类的常用属性
- 数组类的常用方法
 
 
 
 
可空类型
C#提供了一种特殊的数据类型,nullable类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值。声明一个nullable类型(可空类型)的语法如下:
<data_type> > ? <variable_name> = null;
namespace NullAble
{
    class Program
    {
        static void Main(string[] args)
        {
            int? a = null;
            int? b = 123;
            Console.WriteLine("a:{0}, b:{1}", a, b);
        }
    }
}

NULL合并运算符(??)
Null 合并运算符用于定义可空类型和引用类型的默认值。Null 合并运算符为类型转换定义了一个预设值,以防可空类型的值为 Null。
namespace NullAble
{
    class Program
    {
        static void Main(string[] args)
        {
            int? a = null;
            int? b = 123;
            int c = a ?? 234;
            int d = b ?? 234;
            Console.WriteLine("c:{0}, d:{1}", c, d);
        }
    }
}

数组
数组是用来存储数据的集合,通常认为数组是一个同一类型变量的集合。
数组声明
datatype[] arrayname
datatype:这个数组存储数据的类型。
arrayname:数组名。
数组初始化
数组是一个引用类型,所以需要用new关键字来创建数组。
datatype[] arrayname = new datatype[n];
n表示数组的空间大小。
数组赋值
通过下标给指定位置赋值
int[] arr = new int[4];
arr[0] = 1;
声明时进行赋值
int[] arr = {0, 1, 2, 3, 4};
创建并初始化一个数组
int[] arr = new int[5] {1, 2, 3, 4, 5};
省略数组大小进行赋值
nt[] arr = new int[] {1, 2, 3, 4, 5};
数组访问
namespace Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = new int[3];
            arr1[0] = 1;
            int[] arr2 = { 1, 2, 3, 4, 5 };
            int[] arr3 = new int[5] { 1, 3, 5, 7, 9 };
            int[] arr4 = new int[] { 0, 2, 4, 6, 8 };
            int a = arr1[0];
            Console.WriteLine("a:{0}\n", a);
            for(int i = 0; i < 5; i++)
            {
                Console.Write("{0} ", arr2[i]);
            }
            Console.WriteLine("\n");
            foreach (int i in arr3)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine("\n");
        }
    }
}

多维数组
多维度存储相同类型数据的数组。
namespace Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr = new int[5, 2] { { 0, 1 }, { 2, 3 }, { 4, 5 }, { 6, 7 }, { 8, 9 } };
            for(int i = 0; i < 5; i++)
            {
                for(int j = 0; j < 2; j++)
                {
                    Console.Write("{0} ", arr[i, j]);
                }
                Console.WriteLine(" ");
            }
        }
    }
}

交错数组
交错数组是存放数组的数组。
namespace Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int[][] arr = new int[][] { new int[] { 0, 1 }, new int[] { 2, 3 }, 
                                       new int[] { 4, 5 }, new int[] { 6, 7 }, new int[] { 8, 9 } };
            for(int i = 0; i < 5; i++)
            {
                for(int j = 0; j < 2; j++)
                {
                    Console.WriteLine("{0} ", arr[i][j]);
                }
            }
        }
    }
}

数组类
数组类的常用属性
| 属性 | 描述 | 
| IsFixedSize | 获取一个值,该值指定数组是否有固定大小 | 
| IsReadOnly | 描述获取一个值,该值指示数组是否只读 | 
| Length | 获取一个32位整数,返回数组长度 | 
| LongLength | 获取一个64位整数,返回数组长度 | 
| Rank | 获取数组的秩(维度) | 
数组类的常用方法
| 方法 | 描述 | 
| Clear(Array, index, Length) | 根据元素的类型,设置数组从下标index开始,长度为length的元素为零、为false或者为null | 
| Copy(Array, Array, Int32) | 从数组的第一个元素开始复制N个元素到另一个数组 | 
| CopyTo(Array, Int32) | 把源数组全部拷贝到目标数组,从目标数组的第N位开始覆盖 | 
| GetLength(dimension)/GetLongLength(dimension) | 获取数组指定维度的长度,返回32喂整数或64位整数 | 
| GetLowerBound | 返回数组指定维度的第一个元素的索引 | 
| GetUpperBound | 返回数组指定维度的最后一个元素的索引 | 
| Reverse(Array) | 逆转整个一维数组 | 
| SetValue(Object, Int32) | 给一维数组中指定位置的元素设置值 | 
| Sort(Array) | 排序 | 
namespace MyArray
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = new int[5] { 1, 2, 3, 4, 5 };
            int[] arr2 = new int[5] { 1, 2, 3, 4, 5 };
            Array.Clear(arr1, 1, 3);
            foreach(int i in arr1)
            {
                Console.Write("{0} ", i) ;
            }
            Console.WriteLine(" ");
            Array.Copy(arr1, arr2, 3);
            foreach (int i in arr2)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine(" ");
            int[] arr3 = new int[10];
            arr1.CopyTo(arr3, 2);
            foreach (int i in arr3)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine(" ");
            int[,,] arr4 = new int[1, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } } };
            Console.WriteLine("一维:{0},二维:{1},三维:{2}", arr4.GetLength(0), arr4.GetLength(1), arr4.GetLength(2));
            Console.WriteLine("一维:{0},二维:{1},三维:{2}", arr4.GetUpperBound(0), arr4.GetUpperBound(1), arr4.GetUpperBound(2));
            Array.Reverse(arr1);
            foreach (int i in arr1)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine(" ");
            arr1.SetValue(2, 2);
            foreach (int i in arr1)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine(" ");
            Array.Sort(arr1);
            foreach (int i in arr1)
            {
                Console.Write("{0} ", i);
            }
            Console.WriteLine(" ");
        }
    }
}



















