1.c#数组赋值
https://wenku.baidu.com/view/d1f991d64593daef5ef7ba0d4a7302768e996f29.html
C#中数组复制有多种⽅法
数组间的复制,int[] pins = {9,3,4,9};int [] alias = pins;这⾥出了错误,也是错误的根源,以上代码并没有出错,但是根本不是复制,因
为pins和alias都是引⽤,存在于堆栈中,⽽数据9,3,4,3是⼀个int对象存在于堆中,int [] alias = pins;只不过是创建另⼀个引⽤,alias和
pins同时指向{9,3,4,3},当修改其中⼀个引⽤的时候,势必影响另⼀个。复制的意思是新建⼀个和被复制对象⼀样的对象,在C#语⾔
中应该有如下4种⽅法来复制。
⽅法⼀:使⽤for循环
int []pins = {9,3,7,2}
int []copy = new int[pins.length];
for(int i =0;i!=copy.length;i++)
{
copy[i] = pins[i];
}
⽅法⼆:使⽤数组对象中的CopyTo()⽅法
int []pins = {9,3,7,2}
int []copy2 = new int[pins.length];
pins.CopyTo(copy2,0);
⽅法三:使⽤Array类的⼀个静态⽅法Copy()
int []pins = {9,3,7,2}
int []copy3 = new int[pins.length];
Array.Copy(pins,copy3,copy.Length);
⽅法四:使⽤Array类中的⼀个实例⽅法Clone(),可以⼀次调⽤,最⽅便,但是Clone()⽅法返回的是⼀个对象,所以要强制转换成
恰当的类类型。
int []pins = {9,3,7,2}
int []copy4 = (int [])pins.Clone();
⽅法五:
string[] student1 = { "$", "$", "c", "m", "d", "1", "2", "3", "1", "2", "3" };
string[] student2 = { "0", "1", "2", "3", "4", "5", "6", "6", "1", "8", "16","10","45", "37", "82" };
ArrayList student = new ArrayList();
foreach (string s1 in student1)
{
student.Add(s1);
}
foreach (string s2 in student2)
{
student.Add(s2);
}
string[] copyAfter = (string[])student.ToArray(typeof(string));
两个数组合并,最后把合并后的结果赋给copyAfter数组,这个例⼦可以灵活变通,很多地⽅可以⽤”的文档
一、list快速查找索引
ClassifyInfo ci_child = ChildClassifies.Find(row => row.id == ChildClassify.id);
int cur_childindex = ChildClassifies.IndexOf(ci_child);
二、json数组和字符串转化
Newtonsoft.Json.JsonConvert.DeserializeObject<RdContent>(vm.Record_detail.content)
Newtonsoft.Json.JsonConvert.SerializeObject(option),
三、输出调试信息
System.Diagnostics.Debug.WriteLine 输出调试信息
四、io操作阻塞线程(es优化)
出现场景:频繁的使用sql语句,导致方法执行时间边长。
解决方法:可以使用一条sql语句,生成list集合。再从集合中筛选出对应集合
五、热加载
热加载 动态调试方法 接下来就可以不用重启项目来调试了,减少了开发时间
也就是遇到异常、问题继续运行。不过一些动态数据加载不出来
发表评论 取消回复