Tag: noreturn

在C#中是否有类似]的东西来表示编译器该方法永远不会返回值?

对于我的一些代码,我使用了一个如下所示的方法: public static void Throw(string message) where TException : Exception { throw (TException) Activator.CreateInstance(typeof(TException), message); } 我想像这样使用它(简单的例子): public int MyMethod() { if(…) { return 42; } ThrowHelper.Throw(“Test”); // I would have to put “return -1;” or anything like that here for the code to compile. } 现在,显然,我知道MyMethod永远无法返回任何东西,因为它总是(间接地)抛出exception。 但是我当然得到编译器值“并非所有路径都返回一个值”。 这就是为什么我问是否有类似C ++ [[noreturn]]属性的东西,我可以用来向编译器表明代码实际上是有效的 ? 编辑:我想要使​​用抛出辅助类而不是直接抛出或使用exception构建器的原因是这个声明 : 抛出exception的成员不会被内联。 […]