C#方法,可空类型,数组,集合,ArrayList排序,List,Hashtable和Dictionary

C#方法

方法的定义:

public void/int Compare(int a,int b){  }
Program program = new Program();
Console.WriteLine.Compare(a,b);

方法的递归:

public void Test()
{
    Cosole.WriteLine(i);
    i++;
    if(i<=10){
        Test();
    }
}

求阶乘

public int factorial(int num){
   while(num>=1) {
       result = result*num;
       num--;
   }
    return 0;    
}

递归求阶乘

public int factorial(int num){
   int result = 1;
   if(num == 1){
       return 1;
   }
    else {
        result = factorial(num - 1)*num;
    }  
}

方法的参数传递

值类型的参数传递,将内存里的数据参数复制,因此当我们对数据进行更改的时候是不会更改原来的数据的。

引用类型的参数传递:能在数值传递的时候对数据进行更改

int a = 10,b = 20;
public int Compare(ref int a,int b){}
Program program = new Program();
Console.WriteLine(program.Compare(ref a,b));

输出类型的参数传递:能实现一个方法有多个返回值

int a = 10,b = 20;
public void OutTest(out int i){}
Program program = new Program();
program.OutTest(out a);
Console.WriteLine(program.OutTest(out a));

C#可空类型

 int? i = null;//加问号表示此变量可空
int a = i??0;//若i为空则将0赋值给a,若i不为空则将i赋值给a;

数组

int [] 数组名;//声明
int[]num = numble;将数组numble赋值给数组num;
数组名 = new int[10{1,2,3,4,6,5,7,8,9,10]; //创建了一个长度为10的数组,并给其赋值。顺序从0开始
Console.WriteLine(numble[0]);通过索引的方式访问数组

使用for循环打印数组内的所有数据

for(int i =0;i<numbers.Length;i++){
    Console.WriteLine(numbers[i])
}

foreach循环

foreach(int i in numbers){
    Console.WriteLine(i);
}

二维数组

int[,] numbers = new int[2,3]{{},{}};
Console.WriteLine(numbers[0,0]);

打印二维数组:

for(int i = 0;i<2;i++){
    for(int j = 0;j<3;j++){
        Console.WriteLine(numbers[i,j]);
    }
    Console.WriteLine();
}

三维数组:

int[,,] nums = new int[1,2,3]{{{1,2,3},{1,2,3},{1,2,3}},{{4,5,6},{4,5,6},{4,5,6}}};//一个一维数组里嵌套了两个二维数组

集合

ArrayList 和 List
ArrayList:
ArrayList arrayList = new ArrayList();//与数组的区别:可以通过索引对指定位置进行添加和移除,无需设置长度和大小,无需在创建的时候指定。
arrayList:Add(10);
arrayList:Add(5);
arrayList:Add(30);
Console.ReadLine(arryList.Count);//通过Count获取当前集合所含元素

foreach(object obj in arrayList){
    Console.WriteLine(obj);
}
ArrayList排序
arrayList.Sort();//对当前元素进行排序;

排序不能跨数据类型。

List
List<int> list = new List<int>();
List.Add(100);//通过一个泛型对集合内的元素进行限定。

List排序

list.Sort();

List移除元素

list.Remove(100);

List清空集合元素

list.Clear();
Hashtable和Dictionary

Hashtable

Hashtable hashtable = new Hashtable();只能通过键来访问
    hashtable.Add("001","c#");
    hashtable.Add("002","unity");
    hashtable.Add("003","Lua");
Console.WriteLine(hashtable["001"]);
foreach(object key in hashtable.Keys){
    Console.WriteLine("key:" + key + "value :" + hashtable[key]);
};

Hashtable移除的方法

hashtable.Remove("001");//有Count可以查询集合内部元素数量
Console.WriteLine("移除前" + hashtable.Count)
foreach(object key in hashtable.Keys){
    Console.WriteLine("key:" + key + "value :" + hashtable[key]);
};
hashtable.Remove("001");
Console.WriteLine("移除后" + hashtable.Count)
foreach(object key in hashtable.Keys){
    Console.WriteLine("key:" + key + "value :" + hashtable[key]);
};

Hashtable添加数据的方法

hashtable.Add(0,1);//通过这条代码添加数据,但是集合内部会比较混乱
Dictionary
Dictionary<string,string> dict = new Dictionary<string,string>();
foreach (string key in dict.Keys){
    Console.WriteLine("key:" + key + "value :" + dict[key]);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值