作者: littleboy 2023-10-14 16:09:22

C#常用操作

c#判断是否为整数

1
equationMgr.Equation[i].Split('=')[1].Trim().All(char.IsNumber)

c#判断是否为数值

1
2
decimal num = 0;
decimal.TryParse((equationMgr.Equation[i].Split('=')[1].Trim()),out num)

字符串

字符串比较

1
2
3
4
5
6
7
8
9
10
11
方法1:s1.CompareTo(s2):
  表示字符串s1与s2进行大小比较,
  s1<s2 s1.CompareTo(s2)结果为:-1
  s1=s2 s1.CompareTo(s2)结果为:0
  s1>s2 s1.CompareTo(s2)结果为:1

方法2:String.Compare(s1,s2)
  表示字符串s1与s2进行大小比较,
  s1<s2 String.Compare(s1,s2)结果为:-1
  s1=s2 String.Compare(s1,s2)结果为:0
  s1>s2 String.Compare(s1,s2)结果为:1

字符串排序

1
2
Array.Sort(string[]);
for (int q = 0; q < string[].Length; q++) { string[q] = string[q]; Console.WriteLine(string[q] + " "); }

排序

冒泡排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string[] sArray={"b","ac","ab","acd","ae","ra",}
string sTemp;
for (int x = 0; x < components.Length; x++)
{
for (int y = x + 1; y < components.Length; y++)
{
if (String.Compare(sArray[x], sArray[y]) == 1)
{
sTemp = sArray[x];
sArray[x] = sArray[y];
sArray[y] = sTemp;
}
}
}

List 常见用法

实例化

1
2
3
4
5
6
7
8
//实例化
List<string> strList = new List<string>();
//统计
strList.Add(string);
//查找
Person temp = personList.Find(t => t.name == "Jack" && t.age > 30 && t.sex == true);
//查同,设置一个数组用来装重复的字符串:
List<string> strList = new List<string>();

用if判断新对象(字符串)是否在strList数组内:

1
2
3
4
5
6
//用if判断对象是否存在,存在则continue跳过循环(进下一个循环)
if (strList.Contains(string)){continue;}
//不存在则进行方法,
Function();
//并且新对象(字符串)添加到判断数组里
else strList.Add(swComponent.GetPathName());
1
2
3
4
5
6
personList.Exists(t => t.name == "John")

foreach(KeyValuePair<int,int> temp in strList)
{
Console.WriteLine("Key = {0}, Value = {1}",temp.Key, temp.Value);
}

List排序

1
2
3
4
5
6
7
8
class Person : IComparable<Person>
{
public string name;
public int age;
public bool sex;
//定义比较方法,按照 age 比较
public int CompareTo(Person other){if (this.age < other.age) {return -1; }return 1;}
}

List 其他方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Count - List元素数量 
Add(T item) - 向List添加对象
AddRange() - 尾部添加实现了 ICollection 接口的多个元素
BinarySearch() - 排序后的List内使用二分查找
Clear() - 移除所有元素
Contains(T item) - 测试元素是否在List内
CopyTo(T[] array) - 把List拷贝到一维数组内
Exists() - 判断存在
Find() - 查找并返回List内的出现的第一个匹配元素
FindAll() - 查找并返回List内的所有匹配元素
GetEnumerator() - 返回一个用于迭代List的枚举器
GetRange() - 拷贝指定范围的元素到新的List内
IndexOf() - 查找并返回每一个匹配元素的索引
Insert() - 在List内插入一个元素
InsertRange() - 在List内插入一组元素
LastIndexOf() - 查找并返回最后一个匹配元素的索引
Remove() - 移除与指定元素匹配的第一个元素
RemoveAt() - 移除指定索引的元素
RemoveRange() - 移除指定范围的元素
Reverse() - 反转List内元素的顺序
Sort() - 对List内的元素进行排序
ToArray() - 把List内的元素拷贝到一个新的数组内
trimToSize() - 将容量设置为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
2
3
4
5
using(SqlConnection 连接对象名 = new SQLConnection( 数据库连接串 ))
{
//打开数据库连接
//对数据库先关操作的语句
}

using 关键字的用法主要有两个,一个是引用命名空间,一个是创建非托管资源对象。

在 .NET 平台上资源分为托管资源和非托管资源,托管资源是由 .NET 框架直接提供对其资源在内存中的管理,例如声明的变量;非托管资源则不能直接由.NET 框架对其管理,需要使用代码来释放资源,例如数据库资源、操作系统资源等。