标签: using

从using()语句中返回是否有任何副作用?

返回从方法值 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语句之前定义事务,在括号获取它的值,然后括号后返回.

将变量定义并返回到使用括号之外是更好的做法还是以任何方式保存资源?

c# using

124
推荐指数
3
解决办法
2万
查看次数

为什么在C#中删除未使用的using指令?

我想知道是否有任何原因(除了整理源代码)为什么开发人员使用UsingsVisual Studio 2008中的"删除未使用"功能?

.net c# using

115
推荐指数
6
解决办法
3万
查看次数

如何使用C#6"使用静态"功能?

我正在研究 C#6 中的几个新功能,特别是 "使用静态".

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博客的例子不正确吗?为什么这不起作用?


该博客文章现已更新,以反映最新的更新,但这是一个截图,以防博客发生故障:

博客

c# static using c#-6.0 visual-studio-2015

113
推荐指数
1
解决办法
5万
查看次数

在处理之前我是否必须关闭()SQLConnection?

根据我在这里关于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# asp.net using sqlcommand sqlconnection

111
推荐指数
4
解决办法
4万
查看次数

可以"使用"多个资源导致资源泄漏吗?

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将泄漏资源,不会被处置.

  • 这是真的?(font4不会被处理掉)
  • 这是否using(... , ...)应该完全避免使用嵌套使用?

c# using using-statement

106
推荐指数
5
解决办法
7516
查看次数

性能如何受未使用的using指令的影响?

每当您创建新页面或项目时,Visual Studio都会自动为您创建使用语句.其中一些你永远不会使用.

Visual Studio具有"删除未使用的使用"的有用功能.

我想知道如果从未访问过的using语句仍然在文件的顶部提到,是否对程序性能有任何负面影响.

.net c# using visual-studio

103
推荐指数
6
解决办法
2万
查看次数

使用{}语句调用return是一个好方法吗?

我只是想知道returnusing块内调用它是安全/好的方法.

对于前者

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)......

  1. scope.Complete()被叫了吗?
  2. 对于示波器的dispose()方法也是如此.

c# using return

80
推荐指数
2
解决办法
3万
查看次数

为什么"使用命名空间X;" 在类/结构级别内是不允许的?

class C {
  using namespace std;  // error
};
namespace N {
  using namespace std; // ok
}
int main () {
  using namespace std; // ok
}
Run Code Online (Sandbox Code Playgroud)

编辑:想知道背后的动机.

c++ namespaces using language-lawyer

77
推荐指数
4
解决办法
5万
查看次数

我什么时候应该在C#中使用"使用"块?

是否存在我应该(或不应该?)使用"使用"块的特定情况:

using(SomeType t = new SomeType()){
    ...
}
Run Code Online (Sandbox Code Playgroud)

.net c# using

70
推荐指数
4
解决办法
8万
查看次数

为什么我应该使用"using"关键字来访问我的基类方法?

我写下面的代码是为了解释我的问题.如果我注释第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)

c++ oop inheritance using

66
推荐指数
4
解决办法
4万
查看次数