在下面的例子中,返回pen是否会被销毁(处置)?
' VB'
Public Function GetPen() As System.Drawing.Pen
Using pen As New System.Drawing.Pen(_Color, _Width)
pen.DashStyle = _DashStyle
Return pen
End Using
End Function
// C#
public System.Drawing.Pen GetPen()
{
using (System.Drawing.Pen pen = new System.Drawing.Pen(_Color, _Width))
{
pen.DashStyle = _DashStyle;
return pen;
}
}
Run Code Online (Sandbox Code Playgroud)
[编辑]
再精确一点...... Pen对象是通过引用发送给GetPen的调用者还是像结构一样'克隆'?我知道,这是一个类,但对于GDI对象,我无法确定...
将它被破坏(设置)的pen中创建GetPen()当外部方法将其废弃pen用得到GetPen()?
在"使用"块内重新初始化是一个坏主意,在任何时候都要避免.我还是会问这个:
为什么"使用"调用处理原始值而不是最后一个引用或重新初始化(如果使用try finally block则会发生这种情况)
MyClass b = new MyClass();// implements Idisposable
MyClass c = new MyClass();
MyClass a ;
using (a = new MyClass())
{
a = b;
a = c;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,dispose将在原始引用上调用,而不是在引用时更新.这可以通过在dispose方法中在控制台上打印一些内容来轻松验证.
然而,使用try {} finally代码调用最后一个引用dispose方法.
try
{
a = new MyClass();
a = b;
a = c;
}
finally
{
a.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
MSDN:using语句确保即使在对象上调用方法时发生异常,也会调用Dispose.
using (Font font1 = new Font("Arial", 10.0f))
{
byte charset = font1.GdiCharSet;
}
Run Code Online (Sandbox Code Playgroud)
基本上"使用"转换为:
{
Font font1 = new Font("Arial", 10.0f);
try
{
byte charset …Run Code Online (Sandbox Code Playgroud) using (Font font3 = new Font("Arial", 10.0f),
font4 = new Font("Arial", 10.0f))
{
// Use font3 and font4.
}
Run Code Online (Sandbox Code Playgroud)
我知道在using子句中可以使用多个相同类型的对象.
我不能在using子句中使用不同类型的对象吗?
好吧,我试过,但虽然他们是不同的名称和不同的对象,他们的行为相同=有相同的方法集
有没有其他方法可以使用不同类型的使用类?
如果没有,最合适的使用方法是什么?
我一直在查看几个背景工作者的例子,我遇到了类似于此的代码
public class MyClass
{
public MyClass()
{
using(BackgroundWorker _Worker = new BackgroundWorker { WorkerReportsProgress = true})
{
_Worker.DoWork += (s, args) =>
{
...
};
}
_Worker.RunWorkerAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
我没有像我这样在代码中使用"使用"语句.我在使用Code Rush试用版时碰到了类似的东西,这让我回到这个代码并质疑我是否应该这样做.请帮助我了解是否/为什么这是最佳做法.谢谢.
想知道以下情况是否会导致任何内存泄漏.
Aspx页面包含以下内容.
private void Generator(input)
{
using (MemoryStream memoryStream = Helper.Instance.Generate(input))
{
}
}
Run Code Online (Sandbox Code Playgroud)
从返回内存流的aspx页面调用下面的方法.
MemoryStream Generate(input)
{
MemoryStream stream = new MemoryStream();
//doing some stream manipulation here
return stream;
}
Run Code Online (Sandbox Code Playgroud) 是否可以在不安全的指针类型上创建别名,即我想使用以下内容:using bytePtr = System.Byte*.
.net开发人员会在未来版本中引入此功能(指针和数组类型的别名)吗?
在我正在研究的项目中进行编码时,我发现了一些非常奇怪的东西:
namespace detail {
struct tuplelike_tag { };
struct arraylike_tag { };
template<typename>
struct call_with_traits;
template<typename... Ts>
struct call_with_traits<std::tuple<Ts...>> {
using tag = tuplelike_tag;
enum { size = sizeof...(Ts) };
};
template<typename T, std::size_t Sz>
struct call_with_traits<std::array<T, Sz>> {
using tag = arraylike_tag;
enum { size = Sz };
};
template<typename T, std::size_t Sz>
struct call_with_traits<T[Sz]> {
using tag = arraylike_tag;
enum { size = Sz };
};
template<typename F, typename T, int... Is>
auto call_with(F && f, T && …Run Code Online (Sandbox Code Playgroud) 我需要实现以下接口
struct mutex;
struct interface
{
//...
mutex& getMutex();
};
Run Code Online (Sandbox Code Playgroud)
我可以using mutex = ParticularMutex在我的实现中使用直觉,但gcc告诉我:
error: conflicting declaration ‘using mutex = ’
error: ‘class mutex’ has a previous declaration as ‘class mutex’
Run Code Online (Sandbox Code Playgroud)
我没有定义任何两次,只是宣布两次,就像前面宣布的那样,所以
interface?interface定义?用template <typename mutex>?在下面的示例中,如果代码离开using语句后它仍在运行,那么该进程会发生什么?
using (var p = new Process())
{
p.StartInfo.FileName = "c:\\temp\\SomeConsoleApp.exe";
p.Start();
}
Run Code Online (Sandbox Code Playgroud) 我知道“ using”关键字可以用作模板别名和类型别名,但是我没有看到有人提到“ typedef typename”可以被“ using”替换。可以吗