C#常用操作
c#判断是否为整数
1 | equationMgr.Equation[i].Split('=')[1].Trim().All(char.IsNumber) |
c#判断是否为数值
1 | decimal num = 0; |
字符串
字符串比较
1 | 方法1:s1.CompareTo(s2): |
字符串排序
1 | Array.Sort(string[]); |
排序
冒泡排序
1 | string[] sArray={"b","ac","ab","acd","ae","ra",} |
List 常见用法
实例化
1 | //实例化 |
用if判断新对象(字符串)是否在strList数组内:
1 | //用if判断对象是否存在,存在则continue跳过循环(进下一个循环) |
1 | personList.Exists(t => t.name == "John") |
List排序
1 | class Person : IComparable<Person> |
List 其他方法
1 | Count - List元素数量 |
Dictionary字典操作
实例化
Dictionary<[key],[value]> mDic = new Dictionary<[key],[value]>();
参考资料
https://blog.csdn.net/mr_five55/article/details/129804455
路径操作
1)获取当前进程的完整路径,包含文件名(进程名)。
代码:string str =this.GetType().Assembly.Location;
结果:result: X:xxxxxxxxx.exe (.exe 文件所在的目录+.exe 文件名)
2)获取新的 Process
组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
代码:string str =
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
结果:result: X:xxxxxxxxx.exe (.exe 文件所在的目录+.exe 文件名)
3)获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
代码:string?str?=?System.Environment.CurrentDirectory;
结果:result: X:xxxxxx (.exe 文件所在的目录)
4)获取当前 Thread
的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
代码:string?str?=?System.AppDomain.CurrentDomain.BaseDirectory;
结果:result: X:xxxxxx (.exe 文件所在的目录+"")
5)获取和设置包含该应用程序的目录的名称。
代码:string?str?=?System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
结果:result: X:xxxxxx (.exe 文件所在的目录+"")
6)获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
代码:string?str?=?System.Windows.Forms.Application.StartupPath;
结果:result: X:xxxxxx (.exe 文件所在的目录)
7)获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
代码:string?str?=?System.Windows.Forms.Application.ExecutablePath;
结果:result: X:xxxxxxxxx.exe (.exe 文件所在的目录+.exe 文件名)
using用法
除了使用异常处理的方式释放资源外,还可以使用 using 的方式释放资源。
具体的语句如下。
1 | using(SqlConnection 连接对象名 = new SQLConnection( 数据库连接串 )) |
using 关键字的用法主要有两个,一个是引用命名空间,一个是创建非托管资源对象。
在 .NET 平台上资源分为托管资源和非托管资源,托管资源是由 .NET 框架直接提供对其资源在内存中的管理,例如声明的变量;非托管资源则不能直接由.NET 框架对其管理,需要使用代码来释放资源,例如数据库资源、操作系统资源等。