c#:键值对、泛型集的使用、获取文件的路径 、装箱与拆箱、字典创建与运用
键值对的使用:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _04键值对的使用
{
class Program
{
static void Main(string[] args)
{
//创建键值对对象,首先需要添加键值对的键,再添加值,键值对中的键是唯一的,是不能重复的。
Hashtable ht = new Hashtable();
//给键值对添加元素
ht.Add(1, "张三");
ht.Add(2, 3);
//ht.Add(2, 4);这种赋值的方式是一种错误。因为已经具有值的键。
ht[2] = "2号键改变后的键值";
ht.Add("abc", "234234");
ht.Add(false, true);
ht[1] = "李四"; //对键值对进行从新赋值
//ht.Remove(2); //通过键移除键值
//ht.Clear();//清空键值对中的键值
//对于键值对的遍历首先需要取得键值对的键,在通过键去取值,使用关键之foreach()
foreach (var item in ht.Keys) //var关键字通过内容判断类型,注意键值对的索引是从1开始的,Keys表示求出键值对对象的键
{
//需要注意item用于储存从键值对中取出的键值
Console.WriteLine("键值{0}的内容是{1}",item,ht[item]);
}
Console.ReadKey();
}
}
}
运行结果:
/*知识点:
* 1.键值对中的键值可以是任意的数据类型
* 2.对于键值对中添加元素使用的方法通常是Add();不能使用该方法添加具有相同的键的键值
* 3.可是使用与数组赋值的方式给键值对从新赋值,将会首先判断键中是否具有键值,如果有将会将其覆盖
* 4.foreach()方法在进行大量循环的时候,将会有效的缩短运行的时间
*/
泛型集合的使用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _07泛型集合
{
class Program
{
static void Main(string[] args)
{
//创建一个泛型集合对象
List<int> list = new List<int>();
//给泛型集合对象添加元素
list.Add(1);
list.Add(2);
list.Add(3);
list.AddRange(new int[] { 4, 5, 6, 7, }); //给泛型集合添加一个数组
//泛型集合转化成为数组
int [] a= list.ToArray(); //将泛型集合转化成为数组
Console.WriteLine("遍历数组:");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
//将字符数组转化成为集合
char[] cha = new char[] { '你','好' }; //创建一个字符类型的数组
List<char > list1= cha.ToList<char >();
Console.WriteLine("遍历通过字符数组转化后的集合:");
for (int i = 0; i < list1.Count; i++)
{
Console.WriteLine(list1[i]);
}
//遍历泛型集合
Console.WriteLine("遍历泛型集合:");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
Console.ReadKey();
}
}
}
/*知识点:
* 1.泛型集合相对与数组的唯一的优点就是其大小可以任意的改变
* 2.泛型集合在声明的时候必须声明其变量的类型
* 3.数组和泛型集合之间可以相互转换
*
*/
运行结果:
获取文件路径:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _05获取路径path
{
class Program
{
static void Main(string[] args)
{
string str = @"D:\学习视频\ptf\zhang.txt";
//Path是一个静态的类,不能进行对象的创建
Console.WriteLine(Path.GetFileName(str));//获取文件的名称
Console.WriteLine(Path.GetExtension(str));//获取文件的扩展名称
Console.WriteLine(Path.GetFullPath(str));//获取文件总路径
Console.WriteLine(Path.GetPathRoot(str)); //获取文件的根路径
Console .ReadKey();
}
}
}
运行结果:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _06文件操作
{
class Program
{
static void Main(string[] args)
{
#region File函数的基本操作
// File.Create(@"D:\桌面转移\重点总结\lianxi.txt"); //创建文件夹
// Console.WriteLine("文件创建成功");
// File.Copy(@"D:\桌面转移\重点总结\lianxi.txt",@"D:\桌面转移\重点总结\4455.txt"); //文件的复制
//File.Delete(@"D:\桌面转移\重点总结\4455.txt");
//File.Delete(@"D:\桌面转移\重点总结\lianxi.txt");
//Console.WriteLine("文件删除成功");
// File.Create(@"D:\桌面转移\重点总结\4455.txt"); //创建文件夹
#endregion
#region 字符串与字节之间的转化,以及通过字节的形式对文件进行读取,多用与对于音频文件的操作
// string str = "sdfsdfwefwef";//创建一个字符串
// byte[] b = Encoding.Default.GetBytes(str); //将字符串传唤成为byte类型的数组
// File.WriteAllBytes(@"D:\桌面转移\重点总结\4455.txt",b); //创建一个文件夹,读取文件夹中写入内容,如果该文件夹已经存在,则将会覆盖原来的文件夹
// string path = @"D:\桌面转移\重点总结\4455.txt"; //获取文件的路径
//byte[] b1= File.ReadAllBytes(path);//读取指定路径中文件夹中的内容
//// string str1 = b.ToString(); //不能使用这样的方法将其转换成为字符串,在转化的时候需要选择编码的格式
// string str2 = Encoding.Default.GetString(b1);//这样在能将字节数组转化成为字符串
// Console.WriteLine(str2);
// Console.ReadKey();
#endregion
#region 以字符串行的形式对文本进行读取与写入
//以每行的形式读取文件
//string[] str3 = File.ReadAllLines(@"C:\Users\张旗\Desktop\readLines.txt");
//foreach (string item in str3) //对于字符串数组进行循环,是将循环中的内容取出放在item只读变量中
//{
// Console.WriteLine(item);
//}
//以每行的形式写入文件
//string[] str5 = { "asjidasd", "4353535", "gsdg", "你好" };//定义字符串数组
//File.WriteAllLines(@"C:\Users\张旗\Desktop\wirtLines.txt", str5);
//Console.WriteLine("文本写入完成");
//Console.ReadKey();
#endregion
#region 通过文本的形似读取和写入文件
//string str4= File.ReadAllText(c);
// Console.WriteLine(str4);
string str6 = "asdhajsdhjwehdjwhe dwehfkjahskj年后娘好的地欸黑蝴蝶花都得等会而蝴蝶海盗活动奥尔恩的红二代"; //随意定义的字符串
File.WriteAllText(@"D:\桌面转移\text.txt", str6);
Console.WriteLine("文本写入成功");
//读取文本
//这里需要特别的注意,在上面对文件text进行了写入的操作,此时已经打开文件,如果需要在同一个项目中读取该文件,那么首先需要关闭该文件,否者将会出现打开文件失败
//string str7= File.ReadAllText(@"D:\桌面转移\text.txt");//此行代码是错误的实例,因为上面已经对该文件进行打开了。
File.AppendAllText("1.txt","有没有把你覆盖掉");
string str8 = File.ReadAllText("1.txt"); //注意;1.txt文件必须与可执行文件的路径相同,与这个工程的.exe路径相同 //非常重要
Console.WriteLine(str8);
Console.WriteLine(str6);
Console.ReadKey();
#endregion
}
}
}
/*知识点;
* 1.在对某一路径的文件操作的时候,不能同时具有相同的路径进行操作。
* 2.文件被创建以后即便是多次运行程序,文件不会被重复的创建。
* 3.将字符串转换成为某一个数组,在转换的时候需要选择字符串在转化成为新的数组的时候的编码的格式,在没有特殊要求的时候我们选择的是默认的结构
*
*/
简单的理解装箱与拆箱:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _08装箱_拆箱
{
class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList(); //创建一个集合对象
List<int> list1 = new List<int>();//创建创建一个泛型集合
Stopwatch s = new Stopwatch(); //创建计时对象
s.Start();//开始计时
//装箱计时结果:00:00:01.1735782
//不装箱计时结果: 00:00:00.0951248
for (int i = 0; i < 10000000; i++)
{
list.Add(i); //这里进行了装箱的工作。将int类型的数据赋值给objext类型
//list1.Add(i); //这里不是装箱的过程。
}
s.Stop();//结束计时
Console.WriteLine(s.Elapsed);
Console.ReadKey();
}
}
}
/*知识点;
* 1.装箱是将值类型的变量赋值给引用类型
* 2.拆箱是将引用类型的变量转化成为值类型的变量
* 3.装箱与拆箱发生的前提是其两者具有继承的关系
* 4.装箱与拆箱是比较浪费时间的,所以在进行编写程序的时候尽量的不要进行装箱与拆箱的工作
*/
字典的创建以及使用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _09Dictionnary
{
class Program
{
static void Main(string[] args)
{
//Dictionary<int, string> dic = new Dictionary<int, string>(); //创建字典对象,需要声明键与键值的类型
//dic.Add(1, "你好!");
//dic.Add(2, "中国人");
//dic.Add(3, "我是程序员");
////遍历字典中的内容
//foreach (KeyValuePair<int, string> kv in dic)
//{
// Console.WriteLine("{0}-----------------{1}",kv.Key,kv.Value);
//}
//Console.ReadKey();
#region 将 sadaksdjkefwefj字符串放在集合中去,并且判断出现了几次
string str = "sadaksdjkefwefj";
//创建一个字典对象
Dictionary<char , int > dic = new Dictionary<char, int>();
for (int i = 0; i < str.Length; i++)
{
if (dic.ContainsKey(str[i])) //判断字典中的键是否已经被包含,如果被包含,那么就对该键对应的进行加一
{
dic[str[i]]++;
}
else {
dic[str[i]] = 1;
}
}
foreach (KeyValuePair<char ,int > kv in dic)
{
Console.WriteLine("键{0}出现的次数{1}",kv.Key,kv.Value);
}
Console.ReadKey();
#endregion
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix