coderushforroslyn-117626-coding-assistance-code-providers-convert-to-function.md
This Code Provider is used to convert a method that returns void , into a function that returns a value of the appropriate type. It is useful for instance when you need to confirm that the execution was successful by returning a boolean variable.
Note
Convert to Function is a cascading Code Provider. That means that the Code Provider affects all method calls and method declarations in interfaces, base and descendant classes.
Available when the caret is located on a statement that returns a value form the void method.
Specify the value to be returned from the method.
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
Select Convert to Function from the menu.
After execution, the Code Provider changes the method’s return type to the of the value returned from it.
public interface IMessageSender {
string Message { get; }
bool Send();
}
public class GreetingsSender: IMessageSender {
public string Message { get { return "Hello!"; } }
public bool Send() {
Console.WriteLine(Message);
return true;
}
}
Public Interface IMessageSender
ReadOnly Property Message() As String
Function Send() As Boolean
End Interface
Public Class GreetingsSender
Implements IMessageSender
Public ReadOnly Property Message() As String Implements IMessageSender.Message
Get
Return "Hello!"
End Get
End Property
Public Function Send() As Boolean Implements IMessageSender.Send
Console.WriteLine(Message)
Return True
End Function
End Class
Note
This Code Provider is the opposite of Convert to Procedure