C# 基础知识和VS2010的小技巧总汇
2024-09-30 13:14:35
看了一些基础视频,才发现自己的基础比较薄弱,有很多基础知识都不知道。这里总汇一些基础知识。
1:
foreach不仅可以作用于list类的索引集合,还可以遍历dictionary类,这一点比for更简便。
Dictionary<int, long> dary = new Dictionary<int, long>();
dary.Add(, );
dary.Add(, );
dary.Add(, );
string sall = string.Empty; //在vs2010中,打出foreach后,双击tab键,自动跳出后面的语句,非常快速方便。
//这个技巧同样适用于for
foreach (var item in dary)
{
sall += item.ToString() + " ";
}
Console.WriteLine(sall);
Console.ReadLine();
return;
2:使用filestream读取写入文件的操作
public static void CopyFile(string soucre, string target,int length)
{
//1、我们创建一个负责读取的流 使用using关键字可以在读取后立即释放相应的资源 不用额外再写close() dispose()方法
using (FileStream fsRead = new FileStream(soucre, FileMode.Open, FileAccess.Read))
{
//2、创建一个负责写入的流
using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = new byte[length];
//因为文件可能会比较大,所以我们在读取的时候 应该通过一个循环去读取
while (true)
{
//返回本次读取实际读取到的字节数 读取后fsread的流自动提升到读取到的最后一位
int r = fsRead.Read(buffer, , buffer.Length);
//如果返回一个0,也就意味什么都没有读取到,读取完了
if (r == )
{
break;
}
//Console.WriteLine(fsRead.Position);
//Console.WriteLine(fsWrite.Position);
fsWrite.Write(buffer, , r);
}
}
}
}
Visual Studio 2010 实用功能总结 这两篇文章总结了大量VS2010的开发小技巧
最新文章
- javascript的canvas绘图的基本用法
- RESTful API URI 设计: 判断资源是否存在?
- Bois设置教程
- CF# Educational Codeforces Round 3 B. The Best Gift
- The ";get"; method should be used when the form is idempotent---正交的两个概念---
- 1057 N的阶乘
- 254. Factor Combinations
- Working——流程关系状态表
- 赵雅智_BroadcastReceiver电话监听
- JavaEE Tutorials (7) - 在会话bean中使用异步方法调用
- 原生Js 两种方法实现页面关键字高亮显示
- SDAU课程练习--problemA(1000)
- Hadoop编译方法
- 【BZOJ4034】【HAOI2015】树上操作
- SSM商城开发学习
- noi2016旷野大作战
- Linux系统命令 3
- 双网卡设置(转:https://www.cnblogs.com/visionfeng/p/5825078.html)
- UI BOL 练习 get value set attr
- contos7忘记root密码怎么办