public object MethodName(ref float y)
{
//method
}
Run Code Online (Sandbox Code Playgroud)
如何为此方法定义Func委托?
Func<a, out b, bool>,只是不编译,如何声明我想要第二个参数是out一个?
我想这样使用它:
public class Foo()
{
public Func<a, out b, bool> DetectMethod;
}
Run Code Online (Sandbox Code Playgroud) if ( (new Func</*out*/ string, bool>( (/*out*/ string uname) => ....
Run Code Online (Sandbox Code Playgroud)
更多细节:这是登录功能的一部分,我只是希望我的lambda函数用out参数更改login-name用户,并告诉我用户登录了bool返回.
我真的明白我可以返回元组,然后得到我的字符串值,但我想要一些个人清晰度的参数.如果用户没有登录,我最好只返回null的字符串,只想知道我是否可以在lambda函数中使用参数.
而且我真的知道在语句位置上带有表达式的代码并不是那么干净但是没有人说我是否对编译器来说真的很糟糕.
我有一个方法签名
bool TryGetItem(string itemKey,out Item item)
Run Code Online (Sandbox Code Playgroud)
我如何封装这个签名
delegate V Func<T,U,V>(T input, out U output)
Run Code Online (Sandbox Code Playgroud)
如在帖子中:Func <T>没有参数?
以下问题和答案解决了在委托中使用out参数的问题:
我需要更进一步.我有几个转换方法(函数),我想利用一个委托.例如,让我们从下面的示例方法开始:
private bool ConvertToInt(string s, out int value)
{
try
{
value = Int32.Parse(s);
return true;
}
catch (Exception ex)
{
// log error
value = 0;
}
return false;
}
private bool ConvertToBool(string s, out bool value)
{
try
{
value = Convert.ToBoolean(s);
return true;
}
catch (Exception ex)
{
// log error
value = false;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
然后我宣布了以下代表:
delegate V ConvertFunc<T, U, V>(T input, out U output);
Run Code Online (Sandbox Code Playgroud)
我想做的是这样的事情(伪代码):
if (do int conversion)
func …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此签名为ExpandoObject分配方法(函数):
public List<string> CreateList(string input1, out bool processingStatus)
{
//method code...
}
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的代码,下面的代码不能编译:
dynamic runtimeListMaker = new ExpandoObject();
runtimeListMaker.CreateList =
new Func<string, bool, List<string>>(
(input1, out processingStatus) =>
{
var newList = new List<string>();
//processing code...
processingStatus = true;
return newList;
});
Run Code Online (Sandbox Code Playgroud)
不幸的是我无法更改CreateList签名,因为它会破坏向后兼容性,因此重写它不是一个选项.我试图通过使用委托解决这个问题,但在运行时,我得到了"无法调用非委托类型"异常.我想这意味着我没有正确分配代表.我需要帮助使语法正确(委托示例也可以).谢谢!!
我有两种方法。它们非常相似。我尝试使用泛型,但不适用于TryParse()
public static int EnterIntengerNumber()
{
while (true)
{
Console.Write("Enter an intenger number: ");
if (int.TryParse(Console.ReadLine(), out int number))
{
return number;
}
else
{
ConsoleError("Incorrect value");
}
}
}
public static double EnterRealNumber()
{
while (true)
{
Console.Write("Enter a number: ");
if (double.TryParse(Console.ReadLine(), out double number))
{
return number;
}
else
{
ConsoleError("Incorrect value");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何合并或重构它们?