1.枚举和结构体类型
enum typeName :Type
{
value1,
value2,
……,
}
使用:typeName varName;
typeName = varName.value2;
struct typeName
{
type value1;
type value2;
……;
}
使用:typeName varName;
结构和枚举都是在主函数外声明。
2.数组
type[] ArrayName;
数组在使用前必须初始化。
1、int[] Ary1={1,2,3,4,5};
2、int[] Ary1 = new int[5];//默认初始化为0
3、int[] Ary1 = new int[5]{1,2,3,4,5};
foreach(baseType name in array)
baseType:参数类型
name:name是自己起的变量名
in:表示在哪个数组集合中
array:已有的数组集合
多维数组:
多维数组的赋值:区别int[][]和int[,]的区别
简单来说 int[][]可以分配长度不同的行 而int[,]不行
3.函数
函数参数
ref:按引用传递 传递之前必须进行赋值 方法声明和方法调用必须显式使用ref
out:也是引用传递,但是可以不进行赋值 方法声明和方法调用必须显式使用ref
params:多个参数类型相同的参数,参数数目可变时使用
class Program
{
private int Add(ref int x,int y)//ref参数的使用
{
x = x + y;
return x;
}
private int Add(int x,int y,out int z)//ref参数的使用
{
z = x + y;
return z;
}
private int Add(params int[] x)
{
int result = 0;
foreach(int xx in x)//复习foreach的用法
{
result += xx;
}
return result;
}
private int add2(int a,int b,int c = 0)//默认参数
{
int d = 0;
d = a + b + c;
return d;
}
static void Main(string[] args)
{
Program p = new Program();
int x = 30;
int y = 40;
int z;
int a = 1, b = 2, c = 3;
Console.WriteLine("ref参数的使用" + p.Add(ref x, y));//70 此时x按引用传递
Console.WriteLine("out参数的使用" + p.Add(x, y, out z));//110 x在上一行变成70
Console.WriteLine("params参数的使用" + p.Add(10, 20, 30, 40));//100
//默认参数的两种调用方式
Console.WriteLine("第一种调用方法:" + p.add2(a,b));//3
Console.WriteLine("第二种调用方法" + p.add2(a,b,c));//6
}
}
· 主函数的必须是public修饰
4.常见的字符串操作(仔细介绍看零基础学C#第六章)
CompareTo(),比较2个字符串,返回值-1、0、1。
Contains(),是否包含指定的子串,返回值bool。
Count(),字符串的元素个数,int。
Equals(),比较字符串是否相等,返回bool。
IndexOf(),指定字符所在的位置。
Length,这是熟悉,表示字符的个数。
Replace(),替换,返回替换后的字符串。
Split,将原字符串按照指定的字符进行分割。
ToUpper、ToLower,转换为大小写。
Trim(),去除前后指定的字符,不带参数则表示去除空格。