发布于 2026-01-06 2 阅读
0

5 个(外科手术式)技巧,助您在 C# 中更高效地编程💉 由 Mux 呈现的 DEV 全球展示挑战赛:展示您的项目!

5 个提升 C# 编程效率的实用技巧💉

由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!

编写高效代码并非易事,但并非不可能。无论您偏好何种编程环境,以下五种方法都能帮助您更高效地使用 C# 进行编程。无论您使用 Microsoft Visual Studio 还是其他 IDE,这些建议都能帮助您快速、轻松、高效地提升编程技能。

起初,这些技巧和建议可能看起来非常基础和荒谬,但我知道很多 C# 开发人员并没有将它们付诸实践,最终浪费了比预期更多的时间,只是阅读和试图理解代码(如果还没有到那个时候,那也很快就会到了)。

但最重要的是,这些技巧将帮助你节省时间,并最大限度地减少错误。要想在程序员这个岗位上取得成功,你需要充分利用时间——而这些技巧正是你需要的!


充分利用记录类型

在编写 C# 代码时,简化代码的一个非常简单的方法是使用record类型。类型提供了一种非常易于阅读的方式来创建对象,尤其是在不可变表示中。

糟糕的方式:

//A lot of code lines
public class ApplicationInfo
{
    public string Name { get; init; }
    public string Path { get; init; }
public ApplicationInfo(string name, string path)
    {
        Name = name;
        Path = path;
    }
}
Enter fullscreen mode Exit fullscreen mode

好方法:

 

//Only one line with record types
public record ApplicationInfo(string name, string path);
Enter fullscreen mode Exit fullscreen mode

这样可以节省很多代码行,也更易于阅读。如果几个月后你或其他开发者偶然阅读这段代码,就能更容易理解。

📚查看微软文章了解更多信息:创建记录类型


避免对象初始化不当

这是许多开发者容易忽略的另一个做法。如果属性没有用大括号括起来定义,代码就会变得难以阅读,从而导致理解代码所需的时间更长。

糟糕的方式:

//Poor code format for reading
Dotnetsafer securityManger = new Dotnetsafer();
securityManger.FindVulnerabilities = true;
securityManger.AppName = "Shield.exe";
Enter fullscreen mode Exit fullscreen mode

好方法:

//Better code format for reading
var securityManger = new Dotnetsafer { 
    FindVulnerabilities = true,
    AppName = "Shield.exe"
};
Enter fullscreen mode Exit fullscreen mode

在这种情况下,正如你所看到的,解决方案很简单。使用对象和集合初始化器可以使代码更易于阅读。

📚查看微软文章了解更多信息:对象和集合初始化器


习惯使用 Var 来定义变量

开发者在定义变量时,常常会因为使用过于具体的类型而使代码变得复杂。这只会增加理解代码时的困惑。

糟糕的方式:

//Difficult to read
List<Repository.Shield.SecureDependencies> listOfSecureRefs = _repo.Shield.LastDependenciesAnalyzed();
Enter fullscreen mode Exit fullscreen mode

好方法:

//Easier to read
var listOfSecureRefs = _repo.Shield.LastDependenciesAnalyzed();
Enter fullscreen mode Exit fullscreen mode

要解决这种不良做法,只需使用var关键字即可。这样一来,最终的代码会更简洁,也更容易阅读。

📚查看微软文章了解更多信息:隐式类型局部变量


字符串插值“$”代替 string.Format()

String.Format()是一种将值从一个字符串插入到另一个字符串的常用方法,但如果我们关注代码的简洁性,它并不是最简洁或最好的方法。

糟糕的方式:

//using string.Format()
var date = DateTime.Now;
string greetings = string.Format("Today is {0}, the time is {1:HH:mm} now.", date.DayOfWeek, date);
Enter fullscreen mode Exit fullscreen mode

好方法:

//using string interpolation
var date = DateTime.Now;
string greetings = $"Today is {date.DayOfWeek}, the time is {date:HH:mm} now.");
Enter fullscreen mode Exit fullscreen mode

我们可以看到,使用字符串插值后,生成的代码更易读。这是一个非常简单的示例代码。在更复杂的实际应用中,您会更加体会到代码的组织性和简洁性带来的好处。

📚查看微软文章了解更多信息:$ - 字符串插值


空合并(??)代替 if-else

乍一看,使用嵌套if-else似乎是进行空值检查最简单的方法。虽然它简单易行,但并非最佳方案。

糟糕的方式:

if (application != null)
{
    if (application.protected != null)
    {
        return application.protected.shieldLastRun;
    }
    else 
    {
        return string.empty;
    }
}
else
{
    return string.empty;
}
Enter fullscreen mode Exit fullscreen mode

好方法:

return application?.protected?.shieldLastRun ?? string.empty;
Enter fullscreen mode Exit fullscreen mode

通过使用空值合并运算符, ??我们可以大大减少代码行数,并使其更易于阅读。

📚查看微软文章了解更多信息:  ?? 和 ??= 运算符

文章来源:https://dev.to/bytehide/5-surgical-tips-to-program-more-efficiently-in-c-3bh6