我有一个foreach循环,我在循环中创建一个字符串,但我想在循环外使用var,是否可能?
List<int> roleIntList = new List<int>();
foreach(var rolenodes in roleIntList){
string myStr = "hello";
}
Run Code Online (Sandbox Code Playgroud)
如果在循环外声明字符串,则可以:
string myStr = null;
List<int> roleIntList = new List<int>();
foreach(var rolenodes in roleIntList){
myStr = "hello";
}
Run Code Online (Sandbox Code Playgroud)
然后,在循环运行后,myStr将包含最后指定的值.
| 归档时间: |
|
| 查看次数: |
3807 次 |
| 最近记录: |