标签: using

将代码重构为using语句

我有一个带有许多方法的dal层,它们都调用存储过程,一些返回列表(所以使用SqlDataReader),其他只有特定值.

我有一个帮助方法,创建SqlCommand:

    protected SqlCommand CreateSprocCommand(string name, bool includeReturn, SqlDbType returnType)
    {
        SqlConnection con = new SqlConnection(this.ConnectionString);
        SqlCommand com = new SqlCommand(name, con);
        com.CommandType = System.Data.CommandType.StoredProcedure;

        if (includeReturn)
            com.Parameters.Add("ReturnValue", returnType).Direction = ParameterDirection.ReturnValue;

        return com;
    }
Run Code Online (Sandbox Code Playgroud)

现在我的平均(过度简化)方法体看起来像:

SqlCommand cmd = CreateSprocCommand("SomeSprocName"); //an override of the above mentioned method
try {
   cmd.Connection.Open();
   using (var reader = cmd.ExecuteReader()) {
       //some code looping over the recors
   }
   //some more code to return whatever needs to be returned
}
finally {
   cmd.Connection.Dispose(); …
Run Code Online (Sandbox Code Playgroud)

c# using sqlcommand sqlconnection

1
推荐指数
1
解决办法
974
查看次数

使用using语句

可能重复:
哪个更好,何时:使用语句或在C#中的IDisposable上调用Dispose()?
我什么时候应该在C#中使用"使用"块?
使用if语句?

正确地说,我将如何使用using声明?我有一个教程打开,我不明白.我可以看到超过1种不同的实现方式.哪种方式正确或偏好?

c# using

1
推荐指数
1
解决办法
982
查看次数

为什么我不应该在标题中加入"using namespace std"?

有人曾暗示不建议在头文件中执行此操作:

using namespace std;
Run Code Online (Sandbox Code Playgroud)

为什么不建议?

它是否会导致像这样的链接器错误:(为了方便而行换行)

error LNK2005: "public: __thiscall std::basic_string<char,struct 
std::char_traits<char>,class std::allocator<char> >::
~basic_string<char,struct std::char_traits<char>,class std::allocator<char> > 
(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) 
already defined in tools.lib(Exception.obj) 
Run Code Online (Sandbox Code Playgroud)

c++ using header-files

1
推荐指数
1
解决办法
1001
查看次数

使用"使用"别名

我想为复杂类型定义自己的别名.我很好奇为什么编译器不能识别已导入的类型.例如:

作品:

using System;
using System.Collections.Generic;

using myCollection = System.Collections.Generic.List
                    <System.Collections.Generic.Dictionary<string, string>>;
Run Code Online (Sandbox Code Playgroud)

错误:

using System;
using System.Collections.Generic;

using myCollection = List<Dictionary<string, string>>;
Run Code Online (Sandbox Code Playgroud)

c# using

1
推荐指数
1
解决办法
318
查看次数

我是否必须在短方法中使用"使用"语句?

我是否必须使用using声明即使在方法中立即处置?或者方法的结尾会导致Dispose所有局部变量(包括图形)的自动化吗?(我问这个是因为我看过Dispose在方法结束时调用的例子,并且想知道这是否真的有必要.)

谢谢.

c# garbage-collection dispose using

1
推荐指数
1
解决办法
189
查看次数

'System.Windows.Forms.FolderBrowserDialog'是一种类型而不是命名空间

有可能我错过了一些非常明显但现在我看不到的东西.我有参考System.Windows.Forms,我有下一个using课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

using System.Windows.Forms;
using System.Windows.Forms.FolderBrowserDialog;
Run Code Online (Sandbox Code Playgroud)

但编译器总是给我下一个错误:

错误CS0138:using namespace指令只能应用于名称空间; 'System.Windows.Forms.FolderBrowserDialog'是一种类型而不是命名空间

c# namespaces using

1
推荐指数
1
解决办法
4387
查看次数

"using"语句中的灾难性故障,即使用(var a = new stream())和a.Something()会引发异常

所以我理解在using语句结束时,调用dispose方法.

如果我使用a会发生什么 'using(var a = new Stream()) { a.SomethingThrowsAnException() }'

流是否仍然通过他处理方法?

我写了一个实现IDisposable的类,它是关键任务,我不会丢失内存中的内容,因此我计划在发生灾难性事件时序列化数据,即我有一个像我正在使用的对象的自定义流.

有什么建议?

谢谢.

.net c# idisposable using finalize

1
推荐指数
1
解决办法
198
查看次数

使用或项目名称调用项目

是否更好地通过其类名使用另一个C#项目的类.像这样

 Buss_Logic.Class1 myClass1 = new Buss_Logic.Class1();
 Buss_Logic.Class2 myClass2 = new Buss_Logic.Class2();
Run Code Online (Sandbox Code Playgroud)

或通过using文件顶部的关键字

 using Buss_Logic; 
Run Code Online (Sandbox Code Playgroud)

c# oop using

1
推荐指数
1
解决办法
37
查看次数

在C++中如何声明std :: array的别名?

在C++/C++ 11中,我们如何声明std :: array的别名?

我的意思是这样的:

template<size_t N> using Array = array<int, N>;

int get(Array A, int index) {
    return A[index];
}
Run Code Online (Sandbox Code Playgroud)

但这导致编译错误:数组不是类型.
什么是正确的方法?非常感谢.

c++ templates stl using c++11

1
推荐指数
1
解决办法
582
查看次数

为什么我的列表框没有填充?

我有这个代码,但它不起作用.我尝试了几个不同的版本,但没有任何工作.我是新手,但仍然不懂一切.

    OpenFileDialog filedialog = new OpenFileDialog();

    private void button3_Click(object sender, EventArgs e)
    {

        filedialog.ShowDialog();
        filedialog.FileOk += filedialog_FileOk;
    }

    void filedialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
    {
        using (StreamReader myStream = new StreamReader(filedialog.FileName))
        {
            string line;
            // Read and display lines from the file until the end of  
            // the file is reached. 
            while ((line = myStream.ReadLine()) != null)
            {
                listBox1.Items.Add(line);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我认为这个编辑器需要很多纯文本.

c# listbox using add streamreader

1
推荐指数
1
解决办法
192
查看次数