C#返回var的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17503134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# method to return var
提问by RBrattas
I like to return var from my method. I manage to use dynamic, but then my try-catch-finally does not work. Could you improve my method to return var? I do not need to use dynamic, I am looking for the best method to return var.
我喜欢从我的方法中返回 var。我设法使用动态,但是我的 try-catch-finally 不起作用。你能改进我的方法来返回 var 吗?我不需要使用动态,我正在寻找返回 var 的最佳方法。
Thank you so much in advance. Rune
非常感谢你。符文
public dynamic m_DEMO_Return_var_method(string vpSchemaName, string vpTableName)
{
var var_List = new List<string>();
//try
//{
DataTable iDataTable = new DataTable();
var_List.Clear();
foreach (DataRow iDataRow in iDataTable.Rows)
{
var_List.Add(iDataRow["COLUMN_NAME"].ToString());
}
var vColumnName = var_List.ToArray();
return vColumnName;
//}
//catch (Exception im_Exception)
//{
// return vColumnName;
//}
//finally
//{
//}
}
采纳答案by captainsac
You can update your code as follows.
您可以按如下方式更新您的代码。
public dynamic m_DEMO_Return_var_method(string vpSchemaName, string vpTableName)
{
var var_List = new List<string>();
var vColumnName = var_List.ToArray();
try
{
DataTable iDataTable = new DataTable();
var_List.Clear();
foreach (DataRow iDataRow in iDataTable.Rows)
{
var_List.Add(iDataRow["COLUMN_NAME"].ToString());
}
vColumnName = var_List.ToArray();
return vColumnName;
}
catch (Exception im_Exception)
{
return null;
}
finally
{
}
return vColumnName;
}
The method will return null if vColumnName variable will not contain any data or if any exception will occure.
如果 vColumnName 变量不包含任何数据或发生任何异常,该方法将返回 null。
回答by Douglas
The type of the object that you are returning is always string[]. You could change the return type of your method without affecting its functionality:
您要返回的对象的类型始终是string[]。您可以在不影响其功能的情况下更改方法的返回类型:
public string[] m_DEMO_Return_var_method(string vpSchemaName, string vpTableName)
This would still allow you to use varwhen calling the method:
这仍然允许您var在调用方法时使用:
var result = m_DEMO_Return_var_method("schema", "table");
Edit: Your catchblock isn't compiling because you're attempting to return a variable, vColumnName, that is not declared within its scope. You can fix this by returning nullor an empty string[].
编辑:您的catch块未编译,因为您试图返回一个vColumnName未在其范围内声明的变量。您可以通过返回null或空的string[].
catch
{
return null;
// return new string[0]; // (alternative)
}
回答by Yair Nevet
Could you improve my method to return var? I do not need to use dynamic, I am looking for the best method to return var.
你能改进我的方法来返回 var 吗?我不需要使用动态,我正在寻找返回 var 的最佳方法。
varis not a type but a C# keyword for shortening and making the code more readable (an implicitly typed local variable) - read hereabout it more deeply. So forget about returning var- Use a real type in your method signature, just like: int, stringetc.
var不是一种类型,而是一个 C# 关键字,用于缩短代码并使代码更具可读性(隐式类型的局部变量) -更深入地阅读此处。所以,不要担心返回var-使用一个真正的类型,你的方法签名,就像:int,string等等。
As it's looked now, you're returning stringarray, so change the return type to string[], look:
现在看起来,您正在返回string数组,因此将返回类型更改为string[],请看:
public string[] m_DEMO_Return_var_method(string vpSchemaName, string vpTableName)
As for the try-catch-finallyblock, it would not work for you because vColumnNameis declared in the tryscope (code block) - You can always declare it higher, in the method scope for example.
至于try-catch-finally块,它对你不起作用,因为它vColumnName是在try作用域(代码块)中声明的——你总是可以将它声明为更高的,例如在方法作用域中。
BTW, you're just instanciating the DataTable but not filling it first, where should the data come from?:
顺便说一句,您只是在实例化 DataTable 而不是先填充它,数据应该从哪里来?:
DataTable iDataTable = new DataTable();
You're also clearing the var_Listwithout filling it first:
您还可以在var_List不先填充的情况下清除它:
var_List.Clear();
In addition, you're better use another name for your method name, as the accepted convention in c# is Pascal case for methods naming:
此外,您最好为方法名称使用另一个名称,因为 c# 中接受的约定是方法命名的 Pascal 大小写:
GetVarMethod
回答by lightbricko
Simply change the return type of the method to object and you can return anything declared using the var keyword whatever type it happens to be.
只需将方法的返回类型更改为 object,您就可以返回使用 var 关键字声明的任何内容,无论它是什么类型。
回答by terrybozzio
Var cannot be used as method return value neither as method parameter.Since you are returning a collection you can make your return type IEnumerable:
Var 不能用作方法返回值,也不能用作方法参数。由于您正在返回一个集合,您可以使您的返回类型 IEnumerable:
public IEnumerable<T> m_DEMO_Return_var_method<T>(string vpSchemaName, string vpTableName)
{
.......
.......
return vColumnName as IEnumerable<T>;
}
回答by shooky
Although this doesn't directly answer your question of "returning var" it does solve your problem, similar answers exist above but applying a method I use often to your exact code:
虽然这不能直接回答您的“返回 var”问题,但它确实解决了您的问题,上面存在类似的答案,但将我经常使用的方法应用于您的确切代码:
public string[] demo(string vpSchemaName, string vpTableName)
{
List<string> string_List = new List<string>();
DataTable iDataTable = new DataTable();
foreach (DataRow iDataRow in iDataTable.Rows)
{
string_List.Add(iDataRow["COLUMN_NAME"].ToString());
}
return string_List.ToArray();
}

