考虑以下代码:
switch ("")
{
case "":
using var s = new MemoryStream();
break;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不会编译并显示以下错误:“A using 变量不能直接在 switch 部分中使用(考虑使用大括号)”。
修复已经在建议中,但我的问题是为什么下面的代码是合法的,但上面的代码不是?为什么 C# 不能只允许前面的代码?
switch ("")
{
case "":
{
using var s = new MemoryStream();
}
// break can be inside or outside the braces
break;
}
Run Code Online (Sandbox Code Playgroud) 当我创建临时表时,我收到一条错误消息,告诉我临时表已经存在.临时表对于会话是唯一的,所以看起来我的连接没有正确关闭,我认为它可能与我在using语句中的return语句有关.
我有以下代码:
using (IDbConnection connection = dbConnectionHandler.CreateConnection())
{
connection.Open();
CreateATempTable();
PopulateTempTable();
DataSet ds = CallStoredProcThatUsesTempTable();
return ds;
}
Run Code Online (Sandbox Code Playgroud)
我在几个地方使用这种代码来创建一个具有相同名称的临时表.
不幸的是,我收到以下错误:There is already an object named '#MyTempTable' in the database.
现在,我知道临时表对于会话是唯一的,因此一旦会话关闭它就会消失.
我认为有三件事可能会导致这种情况......
有谁知道它是哪一个?或者如果它没有想到的东西?
我正在将一些C#代码转换为Java,它包含该using语句.我应该如何在Java中复制此功能?我要使用try,catch,finally块,但我想我会跟你们先检查.
考虑以下功能:
private int GetSomethingFromFile(FileStream fs)
{
using (BinaryReader br = new BinaryReader(fs))
{
fs.Seek(0, SeekOrigin.Begin);
return br.ReadInt32();
}
}
Run Code Online (Sandbox Code Playgroud)
FileStream对象作为参数传入,BinaryReader使用using语句声明.当我尝试使用该FileStream对象时,在调用此函数后,它会抛出System.ObjectDisposedException.为什么FileStream对象与BinaryReader对象一起被丢弃?
我想我可能在这里使用了Using语句错误.写这个更好的方法是什么?
Dim x As New Serialization.XmlSerializer( ... )
Using file As New FileStream(myFile, FileMode.Create)
Using writer As XmlWriter = XmlTextWriter.Create(file)
x.Serialize(writer, myCollection)
End Using
End Using
Run Code Online (Sandbox Code Playgroud)
我已经读过你只应该using在具有.Dispose()(即实现IDisposable)的对象上使用块,这就是为什么我认为应该没有使用"编写器",而是writer.Close()在最后.但"文件"既有a .Dispose()又有a .Close(),所以我使用哪个?该using或file.Close()?
注意:我正在使用XmlWriter,因为我可以自定义输出.我删除了这里的设置.
是否有任何情况下使用不会处理它应该处理的对象?
例如,
using(dbContext db = new dbContext()){ ... }
Run Code Online (Sandbox Code Playgroud)
有没有办法在最后一个}数据库仍然存在之后?
如果出现这种情况怎么办:
object o = new object();
using(dbContext db = new dbContext()){
o = db.objects.find(1);
}
Run Code Online (Sandbox Code Playgroud)
是否有可能o保持db活力?
据我所知,每当我实例化一个实现IDisposable的类时,我都应该使用该using关键字以确保它被正确处理掉.
像这样:
using (SecureString s = new SecureString())
{
}
Run Code Online (Sandbox Code Playgroud)
以上内容对我来说很容易理解 - 我可以s在这些括号内使用但是一旦我离开这些括号,我就再也不能参考了s.范围很容易看到.
但我不明白的是当你使用using没有封闭括号时它是如何工作的.
private void Function()
{
// Some code here
using (SecureString s = new SecureString())
// more code here
}
Run Code Online (Sandbox Code Playgroud)
你根本不需要使用括号...所以...如果using关键字没有括号,我怎么知道我能在哪里使用这个对象以及它在哪里处理?
我有一个任务,将数据设置为FIFO,然后另一个线程逐个读取FIFO内的数据,然后通过网络发送.调用时数据转换为字节数组FIFO.Add,如下所示:
public byte[] ByteArraySerialize()
{
using (MemoryStream m = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(m))
{
writer.Write((int)this.Opcode);
writer.Write(this.Data);
}
return m.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题:在发送方线程从FIFO中读取数据之前,数据是否可能被破坏或处理?我的问题是了解using内部方法:
这是使用using内部方法的方式可能会导致GC删除MemoryStream,在线程读取数据之前,让我们说在数据进入FIFO后几秒或几分钟后?
我应该在return内部还是外部进行using声明?
public IEnumerable<Foo> GetData()
{
using (var db = new DbContext())
{
return db.Foo().ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
要么
public IEnumerable<Foo> GetData()
{
IEnumerable<Foo> foo;
using (var db = new DbContext())
{
foo = db.Foo().ToList();
}
return foo;
}
Run Code Online (Sandbox Code Playgroud) 使用此代码:
Protected Function GetArgValsForCompanyName(coName As String) As String()
Dim args(2) As String
Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"),
cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName", con)
con.Open()
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("@CoName", SqlDbType.VarChar, 50).Value = coName
Using reader As SqlDataReader = cmd.ExecuteReader
While reader.Read
args(0) = reader.Item(0).ToString()
args(1) = reader.Item(1).ToString()
args(2) = reader.Item(2).ToString()
End While
End Using
End Using
Return args
End Function
Run Code Online (Sandbox Code Playgroud)
......我明白了:
Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred …Run Code Online (Sandbox Code Playgroud) using-statement ×10
c# ×7
.net ×2
vb.net ×2
dbconnection ×1
filestream ×1
grammar ×1
identifier ×1
java ×1
return ×1
temp-tables ×1
try-catch ×1