我只想验证一些事情.我相信如果我将using命令应用于SqlDataReader,它将关闭数据读取器并处理它.例如:
Using sdr As SqlDataReader = cm.ExecuteReader()
Dim someInt As Integer = sdr.GetInt32(0)
'other details and actions
End Using
Run Code Online (Sandbox Code Playgroud)
是否会在退出Using代码块后关闭sdr SqlDataReader .(我相信它会,但只是想验证.)
我知道C++/CLI相当于这个C#代码:
using (SomeClass x = new SomeClass(foo))
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
这是:
{
SomeClass x(foo);
// ...
}
Run Code Online (Sandbox Code Playgroud)
但有没有类似简洁和类似RAII的方式来表达这一点:
using (SomeClass x = SomeFunctionThatReturnsThat(foo))
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
要么:
SomeClass x = SomeFunctionThatReturnsThat(foo);
using (x)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
?我最接近的工作示例是:
SomeClass^ x = SomeFunctionThatReturnsThat(foo);
try
{
// ...
}
finally
{
if (x != nullptr) { delete x; }
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不太好.
我创建了一个解决方案Foo.添加了一个名为Foo.Common
Added a console app的类库,用于调用来自调用的库代码ConsoleApp.
我引用了Foo.CommonfromConsoleApp和typed:
using Foo.Common;
public class Program
{
CommonClass c = new CommonClass();
static void Main(string[] args)
{
}
}
Run Code Online (Sandbox Code Playgroud)
并得到这个:
Error 1 The type or namespace name '**Foo**' could not be found (are you missing a using directive or an assembly reference?) Z:\Foo\Solution1\ConsoleApplication1\Program.cs 3 11 ConsoleApplication1
我为什么要这个?
这是怎么回事?
我知道我们可以在一个使用块中创建多个相同类型的实例!但有没有办法可以在一个使用块中嵌套或写入不同类型的实例?
我刚看了这个,到目前为止它似乎是唯一的选择 http://blogs.msdn.com/b/ericgu/archive/2004/08/05/209267.aspx
我刚刚设置了一个TFS(2012)服务器,现在我正在尝试通过TFS Build服务器构建完整的代码(在VS 2010中用.NET 4.0编写).但在我的解决方案中,我还有一个包含链接文件的WCF RIA项目,因为它们也在其他地方使用,并且不可能在WCF/Silverlight中添加对常规.NET二进制文件的引用.
一切都在我的开发机器上构建没有任何问题但是当我全部检查时,创建一个标准的构建定义并运行该构建定义我遇到了以下问题.链接文件usings(例如UsingNamespace)也是由我们构建并在WCF/Silverlight之前构建的其他项目,但是在通过TFS构建服务器构建时弹出以下错误:
找不到类型或命名空间'UsingNamespace'(您是否缺少using指令或程序集引用?)'
我看过这个问题有什么解决办法吗?
编辑1
只是尝试设置Copy to Output Directory链接文件的属性,Copy Always但这仍然给我同样的错误,因为我期待.问题是链接文件放在可以使用的地方,usings但WCF RIA服务无法访问/查找使用.
编辑2
刚刚尝试了我的本地测试TFS,我可以在那里做我想做的事情,在那里我做了一个构建定义,只需要使用链接文件构建项目所需的解决方案.这没有任何问题.然后我在我们的TFS服务器上尝试了相同的新构建定义,它具有与我的测试TFS相同的解决方案,在这里它不起作用.我确定的唯一区别是我的测试TFS是TFS 2012 Update 1,而我的生产TFS还没有更新1.我下周试着安装它.
编辑3
我刚刚将生产TFS更新为Update 1,但它仍然无法使用我的临时构建定义,该定义仅包含使用链接文件构建silverlight应用程序所需的项目.两个工作区在两个服务器上都是相同的,要构建的项目也是相同的.
假设我有一个一次性对象MyDisposable,它将另一个一次性对象作为构造函数参数.
using(MyDisposable myDisposable= new MyDisposable(new AnotherDisposable()))
{
//whatever
}
Run Code Online (Sandbox Code Playgroud)
假设myDisposable不处理它AnotherDisposable内部的处理方法.
这只能正确处理myDisposable吗?还是处理它AnotherDisposable?
有人可以帮我理解下面的代码
public Font MyFont { get; set; }
void AssignFont()
{
using (Font f = new Font("Shyam",2))
{
this.MyFont = f;
}
}
Run Code Online (Sandbox Code Playgroud)
将处置对象分配给MyFont属性是否有效?
与VS2013相比,VS2015中文件顶部自动添加using(或ImportVB人员)指令的行为已发生变化.因此以下代码:
namespace CommanderKeen.Enemies
{
class Dopefish
{
}
}
namespace CommanderKeen
{
using System;
class GameLogic
{
public GameLogic()
{
// I reference the Dopefish class, being in another sub-namespace here,
// and want VS to automatically import this namespace.
Dopefish neverDies = new Dopefish();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在VS2013中,将在using System;指令后添加以下行:
using CommanderKeen.Enemies;
Run Code Online (Sandbox Code Playgroud)
例如,完全限定的命名空间.然而,VS2015将此缩短为:
using Enemies;
Run Code Online (Sandbox Code Playgroud)
...由于这种情况,我的using指令都放在命名空间内.但是,我从来没有看到这是常见的做法,即使using指令以这种(不常见的)方式存在.
我可以通过将我的using指令移到namespace范围之外来解决这个问题,但我真的不想这样做,因为我的整个项目现在都是这样组织的.
有没有人知道这是否可以修复,可能是通过附加组件?
我们的服务器应用程序有几个方法,按顺序调用,遍历20M行结果集并对其进行转换.此管道中的每个方法都存储200多兆字节的数据副本,可预测的RAM和GC性能会受到严重影响.
每种方法都遵循类似的模式:
public HugeCollection1 Step1 (SomeType sourceData)
{
var transformed = new List<RowType>;
using (var foo = InitializeSomethingExpensive(sourceData))
{
foreach (var row in foo)
{
transformed.Add (TransformRow(row));
}
}
return transformed;
}
Run Code Online (Sandbox Code Playgroud)
然后在管道中调用这些方法,例如
var results1 = Step1(sourceData);
var results2 = Step2(results1);
var results3 = Step3(results2);
...
var finalResults = StepN (resultsNMinus1);
return finalResults; // final results
Run Code Online (Sandbox Code Playgroud)
我想将其转换为更加实用的解决方案,迭代原始源数据,而无需将整个数据集保存在RAM中.我想最终得到一个没有任何中间集合的最终结果列表.
如果在管道的每个阶段都没有需要设置,那么解决方案将很简单:只为每一行运行每个转换并仅存储最终结果.
var transformed = new List<SmallResult>;
// TODO: How to set up and ensure teardown of the *other* pipeline steps?
using (var foo …Run Code Online (Sandbox Code Playgroud) using System.Net; // (See Chapter 16)
...
string s = null;
using (WebClient wc = new WebClient()) // why there is no brackets after this using statement
try { s = wc.DownloadString ("http://www.albahari.com/nutshell/"); }
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.Timeout)
Console.WriteLine ("Timeout");
else
throw; // Can't handle other sorts of WebException, so rethrow
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是cuts在一个Nutshell中的副本,我不明白为什么在using语句后缺少{},这是书中的拼写错误(不太可能)还是不需要?因为语法是使用需要跟随{}内的代码块.
我希望这段代码是:
using System.Net; // (See Chapter 16)
...
string s = null;
using (WebClient wc = new WebClient()) // why there is no …Run Code Online (Sandbox Code Playgroud)