C# 将参数更改为返回值
我需要将“out”改为“return”,因为使用起来会更舒适,但我真的不知道如何将这些函数转换为使用“return”而不是“out”。 我有公共类,在这个类中我有bool方法C# 将参数更改为返回值,c#,C#,我需要将“out”改为“return”,因为使用起来会更舒适,但我真的不知道如何将这些函数转换为使用“return”而不是“out”。 我有公共类,在这个类中我有bool方法 public static bool GetParameter(this Dictionary<string, string> context, string parameterName, out int parameter) { string stringParameter
public static bool GetParameter(this Dictionary<string, string> context, string parameterName, out int parameter)
{
string stringParameter;
context.TryGetValue(parameterName, out stringParameter);
return int.TryParse(stringParameter, out parameter);
}
谢谢 您似乎没有使用该方法的bool结果,因此您可以简单地将您的方法编辑为:
public static int GetParameter(this Dictionary<string, string> context, string parameterName)
{
int parameter;
string stringParameter;
context.TryGetValue(parameterName, out stringParameter);
int.TryParse(stringParameter, out parameter);
return parameter;
}
publicstaticintgetParameter(此字典上下文,字符串参数名称)
{
int参数;
字符串参数;
TryGetValue(parameterName,out stringParameter);
int.TryParse(stringParameter,out参数);
返回参数;
}
如果知道参数始终存在,则可以通过这种方式对其进行转换:
public static string GetParameter(this Dictionary<string, string> context,
string parameterName)
{
string stringParameter = context[parameterName];
return int.Parse(stringParameter);
}
公共静态字符串GetParameter(此字典上下文,
字符串参数(名称)
{
string stringParameter=上下文[parameterName];
返回int.Parse(stringParameter);
}
您可以执行以下操作:
public static int? GetParameter(this Dictionary<string, string> context, string parameterName)
{
string stringParameter;
if (!context.TryGetValue(parameterName, out stringParameter))
return null;
int value;
if (!Int32.TryParse(stringParameter, out value))
return null;
return value;
}
如果您不愿意使用
out
参数,我可以想出两个好的选择。不过,我应该说,从你的问题来看,这似乎是最好的方法
我通常认为只有两个原因可以避免这样的试用模式:
private int _personID;
public void SomeFunction()
{
_classInstance.Context.GetParameter("PersonID", out _personID);
}
public static Result<int> GetParameter(this Dictionary<string, string> context, string parameterName)
{
string stringParameter;
context.TryGetValue(parameterName, out stringParameter);
int result;
if (int.TryParse(stringParameter, out result))
return Result<int>.Success(result);
else
return Result<int>.Failure();
}
- 当涉及异步调用时。例如,测试数据库中是否存在某个内容,如果存在,则返回该内容,这对于out参数来说是不好用的
- 当你流利地做某事时,例如在LINQ
bool
的原因是处理错误。因此,您需要找到另一种处理方法
这里的选择实际上取决于您期望的输入类型
使用异常:
最简单的可能就是不处理它们。让它们繁殖起来。如果找不到或不可解析,只需抛出异常即可
使用异常来指导常规应用程序流通常被认为是一种不好的做法,但它可以解释什么是“常规应用程序流”。所以一定要看看你的数据和情况
public static int GetParameter(this Dictionary<string, string> context, string parameterName)
{
string stringParameter = context[parameterName];
return int.Parse(stringParameter);
}
返回具体类型:
这需要一些开销,但它具有out
参数的所有优点,而实际上并不需要它
也就是说,我不确定我是否真的那么喜欢这个。它给你的东西很棒,但你用它做的事让我感觉很沉重。但无论如何,这是另一种选择
public class ParseResult
{
public ParseResult(bool IsSuccess, int Result)
{
this.IsSuccess = IsSuccess;
this.Result = Result;
}
public bool IsSuccess { get; set; }
public int Result { get; set; }
}
public static ParseResult GetParameter(this Dictionary<string, string> context, string parameterName)
{
int ret;
string stringParameter;
if (context.TryGetValue(parameterName, out stringParameter)
&& int.TryParse(stringParameter, out ret))
{
return new ParseResult(true, ret);
}
else
{
return new ParseResult(false, 0);
}
}
公共类解析结果
{
公共解析结果(bool issucess,int Result)
{
this.issucess=issucess;
结果=结果;
}
公共bool issucess{get;set;}
公共int结果{get;set;}
}
公共静态ParseResult GetParameter(此字典上下文,字符串参数名称)
{
int ret;
字符串参数;
if(context.TryGetValue(parameterName,out stringParameter)
&&int.TryParse(stringParameter,out-ret))
{
返回新的解析结果(true,ret);
}
其他的
{
返回新的解析结果(false,0);
}
}
像这样吗
public static int GetParameter(this Dictionary<string, string> context, string parameterName)
{
string stringParameter;
context.TryGetValue(parameterName, out stringParameter);
int parameter;
int.TryParse(stringParameter, out parameter);
return parameter;
}
private int _personID;
public void SomeFunction()
{
_personID = _classInstance.Context.GetParameter("PersonID");
}
publicstaticintgetParameter(此字典上下文,字符串参数名称)
{
字符串参数;
TryGetValue(parameterName,out stringParameter);
int参数;
int.TryParse(stringParameter,out参数);
返回参数;
}
私人国际人格;
公共函数()
{
_personID=_classInstance.Context.GetParameter(“personID”);
}
首先,我想说的是,在绝大多数情况下,您应该通过抛出异常来处理故障情况
但是,有时您希望能够调用一个方法来获取一个可能会失败的值,但不希望抛出异常来表示失败
这显然是一个通用概念,标准.Net库使用TryXXX()
模式解决了这一问题
我个人不喜欢这样,因为这样会使代码更难阅读,还会干扰委托、异步和流畅的接口
因此,我们编写了自己的包装器类来处理这些情况。下面是一个简化示例(实际代码有很多代码契约,还允许可选的描述性错误字符串和异常):
你想返回什么,只是一个整数?
TryParse
的结果重要吗?John,请添加显示您想要实现的目标的代码。如果context
不包含键parameterName
或者该键的值不代表有效整数,您将返回什么,目前还不清楚op希望从这个功能中得到什么,但他如何区分成功和失败呢?@ThorstenDittmar-他没有。但显然他现在也不需要了,他需要吗?根据他发布的代码,我假设他没有,否则他可以按照Matthew Haugen的建议返回null。他在问题中发布的代码中没有这样做并不意味着该功能已经过时。方法的放置方式在出现错误时返回0,这可能也是一个有效值。我会选择Matthew和我建议的可为空的解决方案。不知道-你确定吗?这很好。。。谢谢。。。但是如果我还有两个像这样的函数,例如“string”和“bool”类型的函数,那么它们之间就有冲突了…:/@John,说出方法GetInt、GetBool、GetString,或者看看c#泛型和Convert.ChangeType
,以避免重复代码。我不认为这是一个可爱的模型;我更喜欢压抑
public static int GetParameter(this Dictionary<string, string> context, string parameterName)
{
string stringParameter;
context.TryGetValue(parameterName, out stringParameter);
int parameter;
int.TryParse(stringParameter, out parameter);
return parameter;
}
private int _personID;
public void SomeFunction()
{
_personID = _classInstance.Context.GetParameter("PersonID");
}
public class Result<T>
{
public static Result<T> Success(T value)
{
return new Result<T>(value, true);
}
public static Result<T> Failure()
{
return new Result<T>(default(T), false);
}
private Result(T value, bool succeeded)
{
_value = value;
_succeeded = succeeded;
}
public T Value
{
get
{
// Don't call this on failure!
Trace.Assert(_succeeded);
return _value;
}
}
public bool Succeeded
{
get
{
return _succeeded;
}
}
private readonly T _value;
private readonly bool _succeeded;
}
public static Result<int> GetParameter(this Dictionary<string, string> context, string parameterName)
{
string stringParameter;
context.TryGetValue(parameterName, out stringParameter);
int result;
if (int.TryParse(stringParameter, out result))
return Result<int>.Success(result);
else
return Result<int>.Failure();
}
var personIdRetrieval = _classInstance.Context.GetParameter("PersonID");
if (personIdRetrieval.Succeeded)
_personID = personIdRetrieval.Value;
else
// Handle failure.