目录
折叠代码:
#region 注释
//要折叠的代码
#endregion
//可选中需要包含的代码,点击鼠标右键,点击 外侧代码 ,在弹出的选项中选择
条件编译(条件运行):
在代码最顶端写#define
如
#define aa123
#if (aa123)
Console.WriteLine("yes");
#else
Console.WriteLine("no");
#endif
则会打印 "yes"
若注释//#define aa123,则会打印"no"
全局函数(静态,公共):
在函数前加public static 如:
public static void aaa(){}
字符串格式化:
String.Format
如:
String aaa = String.Format("Hello {0} {1}", 123, 45.67);
Console.WriteLine(aaa);
//输出为 Hello 123 45.67
三元运算符:
语法:
表达式1 ? 表达式2 : 表达式3;
语义:
先执行表达式1,执行完毕,表达式1的结果如果为真,那么执行表达式2,并且这个整体的运算式的结果是表达式2的结果,否则执行表达式3,运算式的结果是表达式3的结果
if(表达式1){
表达式2;
}else{
表达式3;
}
例:
a = 3>4 ? 3 : 4 ; //输出为4。
a = 3<4 ? 3 : 4 ; //输出为3。
队列:
单线程使用:Queue
多线程使用:ConcurrentQueue
多线程对队列操作时 多个线程过于频繁的队列操作,会使Queue报错,使用高效的线程安全队列 ConcurrentQueue 则会避免此问题的发生
正则:
string s1 = "aaad123d";
string regular = @".d";
MatchCollection arr=Regex.Matches(s1, regular);
Console.WriteLine(arr[0]); //输出: ad
Console.WriteLine(arr[1]); //输出: 3d
数组合并(byte[] 合并)
System.Linq;//需要引用System.Core
byte[] b1 = new byte[] { 1, 2, 3, 4, 5 };
byte[] b2 = new byte[] { 6, 7, 8, 9 };
b1 = b1.Concat(b2).ToArray();
//b1为 {1,2,3,4,5,6,7,8,9}
b6 内容如下
数组去重
List<double> li1 = new List<double>(){1,2,3,1,5,6,8,5,6};
HashSet<double> hs = new HashSet<double>(li1); //此时已经去掉重复的数据保存在hashset中
double ss = hs.ElementAt(0);
//ss==1.0
二进制文件读取/写入
写入
byte[] bys;
string fileNameTime = "./123.bin";
FileStream myStream = new FileStream(fileNameTime, FileMode.Append);//创建文件
BinaryWriter myWriter = new BinaryWriter(myStream);
myWriter.Write(bys);//向文件写入二进制值
读取
string path = Application.StartupPath + "\\aaa.bin";
if (!File.Exists(path))
{
MessageBox.Show("文件不存在", "提示", MessageBoxButtons.YesNoCancel);
}
FileStream fs = new FileStream(path, FileMode.Open); //获取流对象
BinaryReader BR = new BinaryReader(fs);
BR.BaseStream.Seek(0, SeekOrigin.Begin); //设置流位置0
if ((long)Convert.ToInt32(BR.BaseStream.Length) != BR.BaseStream.Length)//是否超过int上限
{
MessageBox.Show("内容超量", "提示", MessageBoxButtons.YesNoCancel);
}
byte[] bytesArr1 = BR.ReadBytes(Convert.ToInt32(BR.BaseStream.Length));//读取全部
版本号
使用程序生成时的时间戳作为版本号
string VersionNumber = "V1.2.1";
label1.Text = VersionNumber + "." + System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString("yyyyMMdd");
遍历结构体
[StructLayout(LayoutKind.Sequential, Pack = 1)] // 按1字节对齐
public struct SS_AB //结构体
{
public int aaa;
public float bbb;
};
public Form1()
{
InitializeComponent();
SS_AB ss = new SS_AB() { aaa = 1, bbb = 2 };
FieldInfo[] fieldInfos = typeof(SS_AB).GetFields();
for (int i = 0; i < fieldInfos.Length; i++)
{
FieldInfo FiledFirstLayer = fieldInfos[i];
string Name= FiledFirstLayer.Name;
object Value = FiledFirstLayer.GetValue(ss);
Console.WriteLine(Name+":"+Value.ToString());
}
}
//输出为:
aaa:1
bbb:2
Json序列化
//序列化对象
AAA a1 = new AAA()
//序列化
string jsonstr = JsonConvert.SerializeObject(a1);
//反序列化
AAA a2 = JsonConvert.DeserializeObject<AAA>(jsonstr);
创建数组副本
float[] A = {1.1f,1.2f,1.3f,1.4f};
//被修改
float[] B = A;
B[0] = 666;
Console.WriteLine("d2---");
foreach (var item in A)
{
Console.WriteLine(item);
}
//未修改
float[] C = A.Clone() as float[]; //创建副本!
C[0] = 1212;
Console.WriteLine("d3---");
foreach (var item in A)
{
Console.WriteLine(item);
}
//效果
d2---
666
1.2
1.3
1.4
d3---
666
1.2
1.3
1.4