gpt4 book ai didi

c# - 在 try,catch block 之外运行代码是否有任何性能优势?

转载 作者:太空宇宙 更新时间:2023-11-03 17:08:44 25 4
gpt4 key购买 nike

我很想知道我是否应该尽量减少 try/catch block 中的代码,或者这真的无关紧要。

   public bool ObjectExists(string stringTest, string againSomethingElse)
{
if(true) {}
else {} //Code here is better/worst/same
try
{
//Versus code inside try/catch block

}
catch (Exception)
{

throw;
}
}

最佳答案

在 .net 中,try/catch 仅在实际抛出异常时才会产生开销。所以不要太担心将代码放在 try 中的性能影响。只是不要将异常作为一种流控制形式。

关于c# - 在 try,catch block 之外运行代码是否有任何性能优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1463514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com