[英]“Method must have a return type” and “return must not be followed by an object expression”
Im adding this method to a public static class 我将此方法添加到公共静态类
public static LogMessage(Exception ex)
{
Trace.WriteLine(ex.ToString());
return ex;
}
When I do this, I get a message that says "method must have a return type" 当我这样做时,我收到一条消息,指出“方法必须具有返回类型”
and in the return I also get a message that says "Since 'Util.logMessage(System.Exception)' returns void, a return keyword must not be followed by an object expression" 并且在返回中,我还收到一条消息,指出“由于'Util.logMessage(System.Exception)'返回void,所以return关键字后不能包含对象表达式”
How do I correct this? 我该如何纠正?
You would need to change the declaration to return an Exception: 您将需要更改声明以返回异常:
public static Exception LogMessage(Exception ex)
{
Trace.WriteLine(ex.ToString());
return ex;
}
Note that, depending on usage, it might make sense to allow this to be a generic method: 请注意,根据用法,将其作为通用方法可能很有意义:
public static T LogMessage<T>(T ex) where T : Exception
{
Trace.WriteLine(ex.ToString());
return ex;
}
This would allow you to use the resulting exception in a strongly typed manner. 这将允许您以强类型方式使用结果异常。
Alternatively, you could just not return the exception, since Logging shouldn't need to return an exception in any case: 另外,您不能返回异常,因为在任何情况下日志记录都不需要返回异常:
public static void LogMessage(Exception ex)
{
Trace.WriteLine(ex.ToString());
}
Your method signature has no return type. 您的方法签名没有返回类型。 Based on the code, I'm guessing you want: 根据代码,我猜您想要:
public static Exception LogMessage(Exception ex)
{
Trace.WriteLine(ex.ToString());
return ex;
}
Which really makes no sense. 这真的没有道理。 There's no reason to return the exception after it's logged. 在记录异常后没有理由返回异常。 You really could simply do: 您确实可以简单地做到:
public static void LogMessage(Exception ex)
{
Trace.WriteLine(ex.ToString());
}
public static Exception LogMessage(Exception ex)
{
Trace.WriteLine(ex.ToString());
return ex;
}
If you want to return the exception: 如果要返回异常:
public static Exception LogMessage(Exception ex)
If void: 如果无效:
public static void LogMessage(Exception ex)
Any method that has a return must have the return type specified. 任何具有返回值的方法都必须指定返回类型。
Your situation: 您的情况:
public static Exception LogMessage(Exception ex)
{
Trace.WriteLine(ex.ToString());
return ex;
}
If you wanted to return a string: 如果要返回字符串:
public static string LogMessage(Exception ex)
{
return ex.ToString();
}
The void
modifier that is seen with methods that have no return is simply stating that the return is of type void
. 在没有返回值的方法中看到的void
修饰符只是说明该返回值是void
类型。 In this case your return would be the type of Exception
. 在这种情况下,您的返回将是Exception
类型。 A method should always have a return type, whether it is void
, a standard type such as string
or int
, or an object in the type that your class is, such as MyClass
. 方法应始终具有返回类型,无论该类型是否为void
,标准类型(例如string
或int
)或类属于您的类的对象(例如MyClass
。 It can also return a generic such as List<T>
or object
. 它还可以返回诸如List<T>
或object
的泛型。
In this case though, as others have stated, it really does not make sense to return an exception. 但是,正如其他人所述,在这种情况下,返回异常确实没有任何意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.