标签: using

哪个更快 - 使用块或Try/Catch/Finally

这是一个后续问题,以

我应该坚持使用Try/Catch/Finally构造,还是使用Using构造?

Try/Catch/Finally的示例代码:

Dim oRequest As WebRequest
Dim oResponse As HttpWebResponse = Nothing
Dim dataStream As Stream = Nothing
Dim reader As StreamReader = Nothing
Dim responseFromServer As String

Try
        sNewCustomerURL = NewCustomerQueryStringPrepare()

    'make the call to the webservice to add a new customer
    oRequest = WebRequest.Create(sNewCustomerURL)

    oRequest = CType(oRequesC, HttpWebRequest)
    oRequest.Method = "GET"
    oResponse = CType(oRequest.GetResponse(), HttpWebResponse)

    dataStream = oResponse.GetResponseStream()
    reader = New StreamReader(dataStream)
    responseFromServer = reader.ReadToEnd()

        Dim xml As New XmlDocument()
    xml.LoadXml(responseFromServer)
    Dim node As XmlNodeList = …
Run Code Online (Sandbox Code Playgroud)

vb.net performance using

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

如果使用块返回,是否会丢弃IDisposable?

例如

using(var something = GetSomething())
{
    something.DoSomething();
    if(something.IsX()) return true;
}
return false;
Run Code Online (Sandbox Code Playgroud)

.net c# idisposable using return

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

如何克服C++头文件的命名空间邪恶?

通过我的一个项目,我将进入C++领域.基本上我来自Java背景,并想知道如何在C++世界中实现Java包的概念.这使我了解了名称空间的C++概念.

到目前为止,我对命名空间绝对不错,但是当涉及到头文件时,就完全限定的类名,using-directives和using-declarations来说,事情变得有点低效.

这个问题的一个很好的说明是文章由Herb萨特.

据我所知,这一切归结为:如果您编写头文件,请始终使用完全限定的类型名称来引用其他名称空间中的类型.

这几乎是不可接受的.由于C++标头通常提供类的声明,因此最大可读性具有最高优先级.完全有资格从不同的命名空间每种类型产生大量视觉噪声的,最终削弱了头的可读性,其程度提出了一个问题,即是否在所有使用的命名空间.

不过我想利用C++命名空间,所以想一想:如何克服C++头文件的命名空间邪恶?经过一些研究,我认为typedef可能是解决这个问题的有效方法.

下面你将找到一个C++示例程序,它演示了我如何使用公共类作用域typedef从其他名称空间导入类型.该程序在语法上是正确的,并在MinGW W64上编译良好.到目前为止一切都那么好,但我不确定这种方法是否能乐于从标题中删除using关键字,但会引入另一个我根本不知道的问题.就像Herb Sutter描述的东西一样棘手.

那就是我恳请所有对C++有深入了解的人来审查下面的代码,让我知道这是否有效.谢谢你的想法.

MyFirstClass.hpp

#ifndef MYFIRSTCLASS_HPP_
#define MYFIRSTCLASS_HPP_

namespace com {
namespace company {
namespace package1 {

class MyFirstClass
{
public:
    MyFirstClass();
    ~MyFirstClass();

private:

};

} // namespace package1
} // namespace company
} // namespace com

#endif /* MYFIRSTCLASS_HPP_ */
Run Code Online (Sandbox Code Playgroud)

MyFirstClass.cpp

#include "MyFirstClass.hpp"

using com::company::package1::MyFirstClass;

MyFirstClass::MyFirstClass()
{
}

MyFirstClass::~MyFirstClass()
{
}
Run Code Online (Sandbox Code Playgroud)

MySecondClass.hpp

#ifndef MYSECONDCLASS_HPP_
#define MYSECONDCLASS_HPP_

#include <string>
#include "MyFirstClass.hpp"

namespace com {
namespace company {
namespace package2 {

    /* …
Run Code Online (Sandbox Code Playgroud)

c++ typedef namespaces using header

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

C#using:构造函数以不同的方法

我可以using在使用括号中直接找到C#实例化的所有在线示例:

using (var cnx = new SqlConnection()) { }
Run Code Online (Sandbox Code Playgroud)

我认为以下应该采取相同的行动,但我似乎仍然锁定了资源:

SqlConnection GetConnection() { return new SqlConnection(); }
void foo()
{
    using (var cnx = GetConnection()) { }
}
Run Code Online (Sandbox Code Playgroud)

当我单步执行我的程序并进入使用的右大括号后面的行时,我希望能够以任何方式使用SQL Server Management Studio更改数据库,但我不能.当我关闭我的应用程序时,错误消失了.

这不是隔离到SQL; 我也通过这种方式打开文件流来体验这一点.那就是:超越using块,但操作系统不允许外部应用程序更改文件.

我违反了using合同的某些部分吗?

c# using

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

ADL 是否适用于全局命名空间?

诸如启用std类型输出之类的示例解释了如何使用ADL来“注入”某个函数/运算符,具体取决于 fn/op 应用到的类型。

我想知道 ADL 是否完全适用于全局命名空间,也就是说,using全局命名空间范围内声明(或通过 提供)的类型是否使 ADL在全局命名空间中寻找匹配的函数?

具体来说,这些是等价的。ADL?:

// 1 - at global namespace scope
struct GlobalType {};

template< class Ch, class Tr>
std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, GlobalType const& x)
{
    os << ...;
    return os;
} 

// 2 - within namespace
namespace ecaps {

    struct EcapsType {};

    template< class Ch, class Tr>
    std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, EcapsType const& x)
    {
        os << ...; …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces using global-namespace argument-dependent-lookup

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

`using`声明用于用户定义的文字运算符

是否可以using为文字运算符声明operator ""

例如,

#include <chrono>

namespace MyNamespace
{
  constexpr std::chrono::hours operator "" _hr(unsigned long long n){
    return std::chrono::hours{n};
  }

  // ... other stuff in the namespace ...
}

using MyNamespace::operator"";    // DOES NOT COMPILE!

int main()
{
  auto foo = 37_hr;
}
Run Code Online (Sandbox Code Playgroud)

我的解决方法是将这些运算符放在它们自己的嵌套命名空间中literals,这允许using namespace MyNamespace::literals;,但这似乎有点不优雅,我不明白为什么using指令不能以operator相同的方式用于函数命名空间中的任何其他函数或类型.

c++ namespaces using operator-keyword user-defined-literals

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

在隐式情况下调用 C++ 显式构造函数

我使用 g++ 6.3.0 和 -std=c++14 选项编译了下面的代码。

#include <utility>
#include <iostream>
struct A{
    int x;
    A(const A&)=default;
    A(int x):x(x){}
};

struct B{
  A a;
  template<class... Args>
  B(Args&&... args):a(std::forward<Args>(args)...){
    std::cout<<"1!"<<std::endl;
  }

  explicit B(const A& a):a(a){std::cout<<"2!"<<std::endl;}
};
struct C:B{
  using B::B;
};
int main(){
    A a{2};
    const A& aref=a;
    C c=aref; //Implicit conversion
}
Run Code Online (Sandbox Code Playgroud)

我预计它会输出“1!” 因为转换是隐式的,但它输出“2!”。如果我注释掉模板构造函数,它将无法编译。这是正确的行为,还是某种 g++ bug?

c++ using explicit-constructor language-lawyer

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

c#"using"语句后跟try语句可以在这种情况下对括号进行ombitted吗?

  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)

c# using try-catch

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

Unity 中简化的 using 语句

我使用的是 Unity 2019.2.18f 和 VS 2019 16.4.3(CSC 版本 3.4.1-beta4-19610-02)。我对 Unity 和 C# 都很陌生。

我写了一些这样的代码:

using (UdpClient client = new UdpClient(...)) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

VS 2019 建议该声明可以简化为:

using UdpClient client = new UdpClient(...);
...
Run Code Online (Sandbox Code Playgroud)

我猜这是C# 8.0 的事情。然而,当我这样做时,回到 Unity 控制台,它抱怨声明中缺少括号using

Assets\Scripts\NavioRemote.cs(106,19): error CS1003: Syntax error, '(' expected
Assets\Scripts\NavioRemote.cs(106,101): error CS1026: ) expected
Run Code Online (Sandbox Code Playgroud)

然而,VS 没有报告任何错误。

我有两个问题:

  1. 为什么Unity编译失败而VS认为它是正确的?
  2. 有没有办法让Unity接受这个语法?

c# idisposable using unity-game-engine visual-studio-2019

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

using 命名空间指令是否使名称在内联函数中可用?

考虑声明一个不受约束的ns::operator*. using namespace ns在块作用域中并调用函数之后foo<T>,clangns::operator*在读取内部基于范围的循环的迭代器时使用foo。没有其他类型ns涉及其他类型,因此 ADL 应该不会产生任何候选者。

在以下示例中,static_assert失败并显示消息:

错误:由于要求 'std::is_same_v<const int &, const custom_type &>',静态断言失败

汇编代码显示这ns::operator*是由 clang 使用的。gcc 和 msvc 的断言都通过了!

namespace ns {

template <typename T>
constexpr auto operator*(T&& /*value*/) {
    struct custom_type {};
    return custom_type{};
};

}  // namespace ns

template <typename T>
constexpr void foo() {
    std::vector<T> vec{};
    for (const auto& curr : vec) {
        static_assert(std::is_same_v<const T&, decltype(curr)>);
    }
}

int main() { …
Run Code Online (Sandbox Code Playgroud)

c++ templates namespaces using compiler-bug

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