我使用Substring解码生成的代码以进行激活
但是当用户输入错误的代码(例如1或123)时
C#停止并说“索引和长度必须引用字符串中的位置。”要么 ..
如何忽略它而不检查长度...
在php中,我们可以使用“@”忽略警告和错误
@myfunc(12);
但是在C#中如何?
最佳答案
简短的答案是,你不能。
正确答案是您不应该这样做,并检查输入是否有效。
可怕的答案看起来像这样:
public void SwallowExceptions(Action action)
{
try
{
action();
}
catch
{
}
}
public string DoSomething()
{
string data = "TerribleIdea";
string result = null;
SwallowExceptions(() => result = data.Substring(0, 10000));
return result;
}
关于c# - C#忽略子字符串错误/警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26818874/