1.枚举
枚举的基本类型可以是byte,sbyte,short,ushort,int,unit,long和ulong
默认情况下,每个值都会根据定义的顺序(从0开始)。可以重写赋值过程。
enum typeName:underlyingType
{
value1 = actualVal1,
value2 = actualVal2,
...
valueN = actualValN,
}
另外,还可以使用一个值作为另一个枚举的基础值,为多个枚举指定相同的值。没有赋值的任何值都会自动获得一个初始值。初始值是最后声明的值大1开始的序列。
enum typeName:underlyingType
{
value1 = actualVall,
value2 = value1,
value3,
...
valueN = actualValN
}
2.数组
初始化方式:
int [] myIntArray = {5,9,10,2,99};
int [] myIntArray = new int[5];
//数组大小必须和元素个数相匹配
int [] myIntArray = new int[5]{5,9,10,2,99};
多维数组
double [,] hillHeight = new double[3,4]
double [,] hillHeight = {{1,2,3,4},{2,3,4,5},{3,4,5,6}};
数组的数组:变长数组
//声明
int [][] jaggedIntArray;
//初始化
jaggedIntArray = new int[2][];
jaggedIntArray[0] = new int[3];
jaggedIntArray[1] = new int[4];
jaggedIntArray = new int[3][]{new int[]{1,2,3},new int[]{1},new int[]{1,2}};
int [][] jaggedIntArray = {new int[]{1,2,3},new int[]{1},new int[]{1,2}};
3.引用参数和值参数
//值参数:
static void showDouble(int val){}
//引用参数:
static void showDouble(ref int val){}
ref 变量的2个限制:1.必须是变量。2.必须初始化。
4.输出参数
static int MaxValue(int [] intArray,out int maxIndex){}
ref和out参数的区别:1.不能把未赋值的变量作为ref参数,可以作为out参数。
2.存储在out参数中的变量值会在函数执行过程中丢失。
5.委托
namespace Ch06Ex05
{
class Program
{
delegate double ProcessDelegate(double param1, double param2);
static double Multiply(double param1, double param2)
{
return param1 * param2;
}
static double Divide(double param1, double param2)
{
return param1 / param2;
}
static void Main(string[] args)
{
ProcessDelegate process;
Console.WriteLine("Enter 2 numbers separated with a comma:");
string input = Console.ReadLine();
int commaPos = input.IndexOf(',');
double param1 = Convert.ToDouble(input.Substring(0, commaPos));
double param2 = Convert.ToDouble(input.Substring(commaPos + 1,
input.Length - commaPos - 1));
Console.WriteLine("Enter M to multiply or D to divide:");
input = Console.ReadLine();
if (input == "M")
process = new ProcessDelegate(Multiply);
else
process = new ProcessDelegate(Divide);
Console.WriteLine("Result: {0}", process(param1, param2));
Console.ReadKey();
}
}
}