最有用的.NET实用程序类开发人员倾向于重新发明而不是重用

Ger*_*ton 56 .net

我最近从去年开始阅读这篇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()


Jam*_*ran 36

String.IsNullOrEmpty()


Pan*_*nos 35

Path.GetFileNameWithoutExtension(string path)
Run Code Online (Sandbox Code Playgroud)

返回没有扩展名的指定路径字符串的文件名.

Path.GetTempFileName()
Run Code Online (Sandbox Code Playgroud)

在磁盘上创建唯一命名的零字节临时文件,并返回该文件的完整路径.

  • 该方法仍然名不副实.让我们发明方法Pa​​th.CreateTempfile(),它返回一个临时文件名但实际上并没有创建该文件.由于竞争条件,它会很糟糕,但它的名称与GetTempFileName一样完美. (3认同)
  • _doesn't_创建文件的等效方法称为[`GetRandomFileName`](http://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename.aspx). (2认同)

Joh*_*lan 28

System.Diagnostics.Stopwatch班.


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)

或者附加百分号的人 - 这样的事情.

  • 这不是本地化,而是用错误的数据替换正确的数据.如果是5英镑,你应该显示5英镑,而不是5英镑.5英镑不是5美元. (7认同)
  • @Stimpy,string.Format()更适合本地化. (2认同)

Jam*_*ran 22

Enum.Parse()

  • "reflection => slow"是一种荒谬的泛化和过度简化.要使用该类型,必须已将程序集加载到内存中.因此,这里使用的"反射"只是获得对现有数组的引用.你无法做得更好. (10认同)
  • Toro,你为什么要编程.NET呢?这很慢.. (3认同)

Jam*_*ran 20

String.Join()(然而,几乎所有人都知道string.Split并且似乎每次机会都使用它......)

  • +1.愚蠢的是,我经常看到有人通过创建一个StringBuilder来重新发明轮子,为isFirstElement制作一个布尔值,并通过一个集合进行预处理,在除第一个元素之外的每个元素之前加上",".只需将项添加到List <string>,然后调用ToArray()和string.Join(). (11认同)

Pet*_*lke 20

试图找出我的文档在用户计算机上的位置.只需使用以下内容:

string directory =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Run Code Online (Sandbox Code Playgroud)

  • 在VB.Net中:My.Computer.FileSystem.SpecialFolders.MyDocuments (5认同)

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)

  • 为什么不Path.Combine并完全忽略DirectorySeparatorChar. (3认同)
  • 有些情况下我只需要这个角色 (2认同)

spl*_*tne 13

StringBuilder的类,特别是法AppendFormat.

PS:如果您正在寻找字符串操作性能测量: StringBuilder与使用.NET 2.0的字符串/快速字符串操作


Ed *_*all 12

不要使用Guid生成文件名,只需使用:

Path.GetRandomFileName()
Run Code Online (Sandbox Code Playgroud)


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语法之外使用的所有非常有用的小函数.


Rin*_*lin 9


Gal*_*ian 8

System.Text.RegularExpressions.Regex


mma*_*lay 8

input.StartsWith("stuff") 代替 Regex.IsMatch(input, @"^stuff")

  • 除了input.StartsWith()将执行大约10倍.YAGNI和KISS! (5认同)

Joe*_*orn 8

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.


Dav*_*rab 6

文件的东西.

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)


tor*_*ial 6

System.IO.File.ReadAllText vs使用StreamReader为小文件编写逻辑.

System.IO.File.WriteAllText vs使用StreamWriter为小文件编写逻辑.