我最近从去年开始阅读这篇Phil Haack帖子(最有用的.NET实用程序类开发人员倾向于重新使用而不是重用),并且我认为我会看到是否有人对该列表有任何补充.
Viv*_*vek 47
人们倾向于使用丑陋且必然会失败的以下内容:
string path = basePath + "\\" + fileName;
Run Code Online (Sandbox Code Playgroud)
更好更安全的方式:
string path = Path.Combine(basePath, fileName);
Run Code Online (Sandbox Code Playgroud)
我也看到人们编写自定义方法来读取文件中的所有字节.这个非常方便:
byte[] fileData = File.ReadAllBytes(path); // use path from Path.Combine
Run Code Online (Sandbox Code Playgroud)
正如TheXenocide指出的那样,同样适用于File.ReadAllText()和File.ReadAllLines()
Pan*_*nos 35
Path.GetFileNameWithoutExtension(string path)
Run Code Online (Sandbox Code Playgroud)
返回没有扩展名的指定路径字符串的文件名.
Path.GetTempFileName()
Run Code Online (Sandbox Code Playgroud)
在磁盘上创建唯一命名的零字节临时文件,并返回该文件的完整路径.
RB.*_*RB. 23
的String.Format.
我见过的次数
return "£" & iSomeValue
Run Code Online (Sandbox Code Playgroud)
而不是
return String.Format ("{0:c}", iSomeValue)
Run Code Online (Sandbox Code Playgroud)
或者附加百分号的人 - 这样的事情.
Jam*_*ran 22
Enum.Parse()
Jam*_*ran 20
String.Join()(然而,几乎所有人都知道string.Split并且似乎每次机会都使用它......)
Pet*_*lke 20
试图找出我的文档在用户计算机上的位置.只需使用以下内容:
string directory =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Run Code Online (Sandbox Code Playgroud)
Bob*_*tiz 20
我最近需要在Windows应用程序中下载一些文件.我在WebClient对象上找到了DownloadFile方法:
WebClient wc = new WebClient();
wc.DownloadFile(sourceURLAddress, destFileName);
Run Code Online (Sandbox Code Playgroud)
Joh*_*ran 14
硬编码a/into目录操作字符串与使用:
IO.Path.DirectorySeparatorChar
Run Code Online (Sandbox Code Playgroud)
spl*_*tne 13
该StringBuilder的类,特别是法AppendFormat.
PS:如果您正在寻找字符串操作性能测量: StringBuilder与使用.NET 2.0的字符串/快速字符串操作
Kei*_*ith 10
很多新的Linq功能似乎都很不为人知:
Any<T>() & All<T>()
if( myCollection.Any( x => x.IsSomething ) )
//...
bool allValid = myCollection.All(
x => x.IsValid );
Run Code Online (Sandbox Code Playgroud)
ToList<T>(), ToArray<T>(), ToDictionary<T>()
var newDict = myCollection.ToDictionary(
x => x.Name,
x => x.Value );
Run Code Online (Sandbox Code Playgroud)
First<T>(), FirstOrDefault<T>()
return dbAccessor.GetFromTable( id ).
FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
Where<T>()
//instead of
foreach( Type item in myCollection )
if( item.IsValid )
//do stuff
//you can also do
foreach( var item in myCollection.Where( x => x.IsValid ) )
//do stuff
//note only a simple sample - the logic could be a lot more complex
Run Code Online (Sandbox Code Playgroud)
您可以在Linq语法之外使用的所有非常有用的小函数.
input.StartsWith("stuff") 代替 Regex.IsMatch(input, @"^stuff")
For all it's hidden away under the Microsoft.VisualBasic namespace, TextFieldParser is actually a very nice csv parser. I see a lot of people either roll their own (badly) or use something like the nice Fast CSV library on Code Plex, not even knowing this is already baked into the framework.
文件的东西.
using System.IO;
File.Exists(FileNamePath)
Directory.Exists(strDirPath)
File.Move(currentLocation, newLocation);
File.Delete(fileToDelete);
Directory.CreateDirectory(directory)
System.IO.FileStream file = System.IO.File.Create(fullFilePath);
Run Code Online (Sandbox Code Playgroud)
System.IO.File.ReadAllText vs使用StreamReader为小文件编写逻辑.
System.IO.File.WriteAllText vs使用StreamWriter为小文件编写逻辑.