假设我正在使用#include <iostream>C++,我正在制作一个打印语句.我可以选择:
using namespace std;
[...]
cout << "Hello" << endl;
Run Code Online (Sandbox Code Playgroud)
要么
using std::cout;
using std::endl;
[...]
cout << "Hello" << endl;
Run Code Online (Sandbox Code Playgroud)
要么
std::cout << "Hello" << std::endl;
Run Code Online (Sandbox Code Playgroud)
我被引导相信,也许这是不正确的,第一个有点可以避免,因为它会给你的程序增加许多不必要的膨胀.但是,我不知道第二种和第三种风格在性能方面是否存在任何差异.我看到的大多数使用库的代码都倾向于使用第三种风格; 但对我来说,如果在使用第二个方面没有权衡,它似乎是最干净,最易读的方法,特别是如果你对相关的函数或对象进行大量调用.
任何人都可以开导我吗?
对于某些单元测试,我想将CurrentCulture设置为特定的文化(然后在以后反转这些更改).因为我需要在几个地方做这件事,我正在考虑编写一个CultureChanger类来保存旧文化并在其构造函数中设置新文化,然后在处理过程中重置旧文化.
然后我可以像这样使用它:
using(new CultureChanger(culture)){
//some code and assertions with no references to the CultureChanger
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做,是否有可能在使用块完成之前的某个时间清理CultureChanger?
而使用Using语句会收到此错误
由于已经处理了DbContext,因此无法完成操作.
而使用不使用不接收错误这个错误的原因是什么?
using (var context = new ProCamContext())
{
var q = context.CatCamera.Where(p => p.Category.Equals(value)).Select(p => p.CatDetails);
return Json(q);
}
Run Code Online (Sandbox Code Playgroud) 我试图理解在C#中使用块.我可以在Using语句中创建一个自定义对象,如下所示,它不实现IDisposable接口吗?
Class A
{
}
using(A a = new A())
{
}
Run Code Online (Sandbox Code Playgroud)
它给出了错误,说"错误1'ConsoleApplication1.A':在using语句中使用的类型必须可以隐式转换为'System.IDisposable'"
如何更正此错误?我不想做A类:IDisposable
还有其他方法吗?或者强制要求我们需要在这些使用块内部使用的自定义对象中实现IDisposable Dispose方法?
编辑:我不期望MSDN和数千个网站中的定义.我只是想了解这个错误以及整改
假设我有一个模板:
template<typename T>
struct Foo {int f1, f2;};
Run Code Online (Sandbox Code Playgroud)
我想为它创建一个新的别名.
// This will not work, don't even try:
// using Foo = Bar;
// Instead do like this:
template<typename T>
using Bar = Foo<T>;
Run Code Online (Sandbox Code Playgroud)
哇.它似乎工作.首先.但是......如果我有这样的功能:
// Generic f:
template<template<typename> class Tpl>
void f() {std::cout<<"Generic f"<<std::endl;}
// Specialization of f for Foo:
template<> void f<Foo>() {std::cout<<"f<Foo>"<<std::endl;}
int main() {...; f<Bar>(); ...} //outputs "Generic f"
Run Code Online (Sandbox Code Playgroud)
似乎f <Foo>和f <Bar>是f的不同特化!
所以:
谢谢.
在C#中,如果在"using"块内发生异常,是否会调用Dispose方法?
我正在处理我需要访问数据库的应用程序.使用using语句很好,因为"using" statement is to ensure that the object is always disposed correctly, and it doesn't require explicit code to ensure that this happens.我有点困惑在哪里使用"使用",哪里不使用.
public int route(Route r)
{
try
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using(SqlCommand com = new SqlCommand("",con))
{
using (SqlDataReader sdr = com.ExecuteReader())
{
}
}
}
}
catch (Exception e)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我正在审查一些C#代码,因为我使用过C#已经有一段时间了,所以我想确保我的直觉是正确的.在这段代码中,我看到了一个使用块内的许多地方,其中包括以下内容:
using(StreamWriter thing = new StreamWriter()) {
DataSet DS = SQLQueryMethod();
do stuff to DS and log that stuff to the a log file using thingy...
DS.clear();
DS.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
现在,我已经对此做了一些研究,并且通过我失败的记忆回顾了几年,我认为有很多方法可以做得更好.我想知道哪种方式更"标准"/"最佳".我认为下面的第1号是最好的方法.
提前致谢.
将DataSet添加到using语句中,以便在using语句范围的末尾自动处理它,从而无需clear和dispose.我会这样做:
using(StreamWriter thing = new StreamWriter(), DataSet DS = new DataSet()) {
DS = SQLQueryMethod()
do stuff to DS{...}
}
Run Code Online (Sandbox Code Playgroud)只需在DataSet DS上调用dispose,因为我认为在处理之前清除它是没用的.
实际上有必要以原始方式进行.
我在重构某些代码时遇到了这个问题,这在很大程度上取决于Disposable模式。
我确实有一个只读字段DbContext,正在构造函数中对其进行初始化。现在,我想知道是否可以将字段传递到using块中,以使其以正确的方式处理。还是我必须摆脱字段和初始化才能在每个using块中对其进行初始化
protected readonly DbContext _context;
public Repository(DbContext context)
{
_context = context;
}
public Task<T> GetAsync(Guid id)
{
using (_context)
{
return _context.Set<T>().FindAsync(new CancellationToken(), id);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望以正确的方式应用Dispose模式
在努力将C ++代码移植到更好(更一致)的“现代C ++”使用中,我最近的一轮更改涉及将typedef int32_t I2Arr[2]别名替换为更现代的using I2Arr = int32_t[2]样式。这对于“简单”(标量)类型很好用,对于定义函数指针特别有用:
using IFunc = int32_t(*)(int32_t, int32_t);
Run Code Online (Sandbox Code Playgroud)
看起来比(IMHO)更清晰:
typedef int32_t(IFunc*)(int32_t, int32_t);
Run Code Online (Sandbox Code Playgroud)
但是,我只是为替换typedef实际的函数原型(而不是指向函数的指针)而陷入了困境。例如,我有以下代码,使用“旧样式”:
typedef int32_t MaskMaker(int32_t, const uint8_t *, uint8_t *);
static MaskMaker *maskMakers[maskNum];
Run Code Online (Sandbox Code Playgroud)
现在,也许(可能)我在这里真的很昏暗,但是我无法找到一种将其转换using为别名样式的方法。谁能告诉我该怎么做?