我正在使用ReSharper帮助我发现我的代码中可能存在的错误,虽然不是错误,但它仍然抱怨我应该使用var关键字而不是在声明上显式键入变量.我个人认为,如果我写的话,对我和任何读我代码的人都会更清楚
IList<T> someVar = new List<T>();
Run Code Online (Sandbox Code Playgroud)
代替
var someVar = new List<T>();
Run Code Online (Sandbox Code Playgroud)
知道两种方式之间没有性能差异,我应该忽略这些提示还是坚持使用var关键字?
它只是一个品味的问题,还是隐含地输入变量的好习惯?
我正在尝试做一些应该很简单的事情,但我遇到了可怕的麻烦。我在 StackOverflow 中尝试了多个类似问题的代码,但无济于事。我试图从澳大利亚政府的 ABN 查询中获取各种信息。这是匿名返回 XML 值:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ABRSearchByABNResponse xmlns="http://abr.business.gov.au/ABRXMLSearch/">
<ABRPayloadSearchResults>
<request>
<identifierSearchRequest>
<authenticationGUID>00000000-0000-0000-0000-000000000000</authenticationGUID>
<identifierType>ABN</identifierType>
<identifierValue>00 000 000 000</identifierValue>
<history>N</history>
</identifierSearchRequest>
</request>
<response>
<usageStatement>The Registrar of the ABR monitors the quality of the information available on this website and updates the information regularly. However, neither the Registrar of the ABR nor the Commonwealth guarantee that the information available through this service (including search results) is accurate, up to date, complete or accept any …Run Code Online (Sandbox Code Playgroud) C#3.0引入了var关键字.编译时,编译器会为您插入正确的类型.这意味着它甚至可以在2.0运行时运行.到现在为止还挺好.但是前几天我发现了一个案例,var关键字将被替换为对象,因此不够具体.说你有类似的东西:
var data = AdoMD.GetData(...); // GetData returns a DataTable
foreach (var row in data.Rows)
{
string blah = (string)row[0]; // it fails since it's of type object
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用行时,IntelliSense和编译器都告诉我它是对象类型.data.Rows是类型的System.Data.DataRowCollection.以下作品:
var data = AdoMD.GetData(...); // GetData returns a DataTable
foreach (DataRow row in data.Rows)
{
string blah = (string)row[0]; // works since it now has the right type
}
Run Code Online (Sandbox Code Playgroud)
这不是一个关于VAR的使用问题,对于一个线程在这里.
我正在使用Visual Studio 2008 SP1顺便说一句.
编辑:现在附加了正确的代码.
可能重复:
在C#中使用var关键字
我在Visual Studio中使用resharper.每当我创建类实例resharper建议我使用var而不是确切类型的类.为什么使用var运算符更合适?
可能重复:
在C#中使用var关键字var关键字
的重点是什么?
我在使用隐式打字而不是显式打字时有点困惑.
正如MSDN在C#编码标准中引用的那样:
当从变量的右侧明显变量的类型时,或者当精确类型不重要时,对局部变量使用隐式类型.
我的问题是,当我们已经知道底层变量类型时,为什么我们需要在这里使用隐式类型?
有什么好处?
请帮助澄清我的疑问.
我过去几乎一直使用隐式类型,但由于有人告诉我使用关键字var是一种不好的做法,我现在尝试使用显式类型。为此,我倾向于使用快捷方式Ctrl+.将任何隐式类型更改为显式类型。
尽管如此,今天我不得不在一个 Foreach 中处理两个列表。为此,我使用了.Zip()方法。我尝试将其隐式类型更改为显式类型,但我不能。我也尝试使用我提到的快捷方式,但由于某种原因没有选项。
var test = enemyTeam.Zip(imageList, (c, i) => new { Champion = c, Image = i });
Run Code Online (Sandbox Code Playgroud)
我也尝试过这种隐式类型但没有用:
IEnumerable<new { CurrentGameParticipant Champion, PictureBox Image }> test = enemyTeam.Zip(imageList, (c, i) => new { Champion = c, Image = i });
Run Code Online (Sandbox Code Playgroud) 我是这样做的:
lock (LockObj)
{
foreach (var o in Oo)
{
var val = o.DateActive;
if (val.AddSeconds(30) < DateTime.Now) Oo.Remove(o);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
收集被修改; 枚举操作可能无法执行
应该怎么做?
可能重复:
C#'var'与特定类型性能
如果我写行,是否有任何性能成本(在类型转换方面等)
SqlConnection c = new SqlConnection(connectionString))
Run Code Online (Sandbox Code Playgroud)
如
var c = new SqlConnection(connectionString))
Run Code Online (Sandbox Code Playgroud)