返回从方法值内 using语句是得到一个DataContext似乎总是工作精细,就像这样:
public static Transaction GetMostRecentTransaction(int singleId)
{
using (var db = new DataClasses1DataContext())
{
var transaction = (from t in db.Transactions
orderby t.WhenCreated descending
where t.Id == singleId
select t).SingleOrDefault();
return transaction;
}
}
Run Code Online (Sandbox Code Playgroud)
但是我总是觉得在打破使用括号之前我应该关闭一些东西,例如在using语句之前定义事务,在括号内获取它的值,然后在括号后返回.
将变量定义并返回到使用括号之外是更好的做法还是以任何方式保存资源?
我想知道是否有任何原因(除了整理源代码)为什么开发人员使用UsingsVisual Studio 2008中的"删除未使用"功能?
using static是一种新的using子句,它允许您将类型的静态成员直接导入范围.
(博客文章的底部)
根据我发现的几个教程,这个想法如下,
而不是:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello world!");
Console.WriteLine("Another message");
}
}
Run Code Online (Sandbox Code Playgroud)
您可以Console使用使用静态类的新C#6功能省略重复的语句:
using System.Console;
// ^ `.Console` added.
class Program
{
static void Main()
{
WriteLine("Hello world!");
WriteLine("Another message");
} // ^ `Console.` removed.
}
Run Code Online (Sandbox Code Playgroud)
但是,这似乎对我没有用.我在using声明中收到错误,说:
"A'
using namespace'指令只能应用于名称空间;'Console'是一种类型而不是名称空间.请考虑使用'using static'指令"
我正在使用visual studio 2015,我将构建语言版本设置为"C#6.0"
是什么赋予了?msdn博客的例子不正确吗?为什么这不起作用?
该博客文章现已更新,以反映最新的更新,但这是一个截图,以防博客发生故障:
根据我在这里关于Disposable对象的另一个问题,我们应该在using块结束之前调用Close()吗?
using (SqlConnection connection = new SqlConnection())
using (SqlCommand command = new SqlCommand())
{
command.CommandText = "INSERT INTO YourMom (Amount) VALUES (1)";
command.CommandType = System.Data.CommandType.Text;
connection.Open();
command.ExecuteNonQuery();
// Is this call necessary?
connection.Close();
}
Run Code Online (Sandbox Code Playgroud) C#允许我执行以下操作(来自MSDN的示例):
using (Font font3 = new Font("Arial", 10.0f),
font4 = new Font("Arial", 10.0f))
{
// Use font3 and font4.
}
Run Code Online (Sandbox Code Playgroud)
如果发生什么font4 = new Font抛出?从我的理解,font3将泄漏资源,不会被处置.
using(... , ...)应该完全避免使用嵌套使用?每当您创建新页面或项目时,Visual Studio都会自动为您创建使用语句.其中一些你永远不会使用.
Visual Studio具有"删除未使用的使用"的有用功能.
我想知道如果从未访问过的using语句仍然在文件的顶部提到,是否对程序性能有任何负面影响.
我只是想知道return在using块内调用它是安全/好的方法.
对于前者
using(var scope = new TransactionScope())
{
// my core logic
return true; // if condition met else
return false;
scope.Complete();
}
Run Code Online (Sandbox Code Playgroud)
我们知道最后一个大括号 dispose()将被取消.但是在上面的情况下,由于return将控件跳出给定范围(AFAIK)......
scope.Complete()被叫了吗?dispose()方法也是如此.class C {
using namespace std; // error
};
namespace N {
using namespace std; // ok
}
int main () {
using namespace std; // ok
}
Run Code Online (Sandbox Code Playgroud)
编辑:想知道背后的动机.
是否存在我应该(或不应该?)使用"使用"块的特定情况:
using(SomeType t = new SomeType()){
...
}
Run Code Online (Sandbox Code Playgroud) 我写下面的代码是为了解释我的问题.如果我注释第11行(使用关键字"using"),编译器不会编译该文件并显示以下错误:invalid conversion from 'char' to 'const char*'.它似乎没有void action(char)在Parent类中看到Son类的方法.
为什么编译器会以这种方式运行?或者我做错了什么?
class Parent
{
public:
virtual void action( const char how ){ this->action( &how ); }
virtual void action( const char * how ) = 0;
};
class Son : public Parent
{
public:
using Parent::action; // Why should i write this line?
void action( const char * how ){ printf( "Action: %c\n", *how ); }
};
int main( int argc, char** argv ) …Run Code Online (Sandbox Code Playgroud) using ×10
c# ×8
.net ×3
c++ ×2
asp.net ×1
c#-6.0 ×1
inheritance ×1
namespaces ×1
oop ×1
return ×1
sqlcommand ×1
static ×1