C#10进阶语法(四)

201 阅读3分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第5天,点击查看活动详情

前言

今天我们继续来讲一讲C#进阶语法,以及Dictonary,这也是我们学习.netcore的基础,跟着我一起来看一看吧!

引用类型和值类型

内存有两类:栈内(Cpu直接管理,速度快)和堆内存(由.net管理,非常灵活)

1.struct类型就是值类型:int、double等(不能继承)

2.class类型就是引用类型:string

3.record类型就是引用类型

引用类型和值类型如何存储到内存中:

一、第一种情况
    
 int num =49;  //存储到栈上
 Student stu = new Student(){Id=1}  //变量里面存储了一个地址或者叫引用,实际值存储在堆上
​
​
​
二、第二种情况
    class student
    { //存储在堆上
        public int Id{get;set;}
    }
​
​
struct person
{
    //存储在栈上
    public int Id{get;set;}
    
    //存储在堆上
    public string Name{get;set;}
}

文件获取

1.磁盘信息,DriveInfo

2.系统盘文件信息获取,Enviroment

3.路径管理Path

string path = Path.Combine(Enviroment.CurrentDirectory."Ant");
Console.WriteLine(path)

4.目录管理Directory,DirectoryInfo(需要实例化)

DirectoryInfo info = Directory.CreateDirectory(path) ;
Console.WriteLine(info.Name)

5.文件管理File,FileInfo(需要实例化)

string filePath = Path.Combine(path,"Data.txt");
FileStream filestream = File.Create("FilePath");
​
FileInfo fileinfo = new FileInfo(FilePath);
Console.WriteLine(fileinfo.CreateTime);

6.文件操作Stream,FileStream,StreamReader,StreamWriter

//StreamWriter,非托管类型,.net管不了,操作系统管理,手动释放
using(StreamWriter sw = new StreamWriter(filePath,true))
{
    sw.WriteLine("Ant编程")
}
​
​
StreamReader sr = new StreamReader(filePath);
string temp;
while((temp=sr.ReadLine())!=null){
    Console.WriteLine(temp);
}

Dictionary字典的使用

  • 在声明Dictionary字典时,需要同事为其声明Dictionary字典内的键与值的类型
  • 示例:Dictionary<int, string> dictionary = new Dictionary<int, string>();

键与值可以是任何类型,但是键必须在设置时是唯一的,而值可以不唯一,就好比每个学生的学号必须是唯一的,而所有的成绩可以不唯一。

Dictionary<int, string> dictionary = new Dictionary<int,  string>();
//两种赋值方式  
// 方  式 一 :Add 方 法 赋 值 
dictionary.Add(1, "98分"); 
dictionary.Add(2, "92分");  
dictionary.Add(3, "89分"); 
dictionary.Add(1, "88分");   //系统会报错  
//方式二:索引器赋值  
dictionary[1] = "88分";      //系统不会报错
dictionary[4]  = "99分";  
//方式三:对象初始化器  
Dictionary<string, string> dictionary2 = new  Dictionary<string, string>()  {
    {"A","aa" },  
    {"B","BB" },  
    {"C","CC" }  
};  

注意dictionary[1]方式既可以赋新值可以修改原来已键有的值,类似于数组索引器的使用, 所以可以使用之前已使用过的键。但是Add方法不可以添加已有键的值。

//获取键为1的值  
//方式一:索引器取值  
string value = dictionary[1];  
//方式二:foreach遍历取值  
foreach (KeyValuePair<string, string> item in dictionary) {  
    int key  = item.Key; 
    string value = item.Value;  
}  
//移除键为1的键值对
dictionary.Remove(1);

总结

  • 可以是任何类型,但是键必须在设置时是唯一 的,而值可以不唯一
  • 使用Add()方法添加键值对,不可添加已有的键名
  • 索引模式可以新赋值也可以修改已有的键值

foreach的使用

  • foreach就是传说中的增强for循环或者称作foreach循环
  • foreach对遍历字典或集合具备天然优势,效率高过for循环

3.1 foreach操作数组

int[] ints= {123456};
foreach (int item in ints){  //每次循环,其item都是整型数组中的一个元素  } 

3.2 foreach操作集合

List<int> intList = new  List<int>() { 1, 23, 45, 6 }; 
foreach (int item in ints){  //每次循环,其item都是List集合中的一个元素  }

3.3 foreach操作字典

Dictionary<string, string> dictionary = new Dictionary<string,  string>() {  
    { "A","aa"}, 
    { "B","bb"}, 
    { "C","cc"},
};  
foreach  (KeyValuePair<int, string> item  in dictionary) {
    int key  = item.Key; 
    string value =  item.Value; 
}  
总结:这篇文章是我更新C#10语法的最后一篇,后面可能会更.netcore的文章,大家有兴趣的可能继续看看哦。