447 questions
0
votes
0
answers
111
views
How to set script prerequisites
A while ago I created a small Use-ClassAccessors script and uploaded that to the PowerShell Gallery.
Now I am creating another script that has a dependency on the Use-ClassAccessors script.
Is there ...
2
votes
1
answer
586
views
Should I wrap new HttpRequestMessage() in a using statement?
I found the following code:
using (var requestMessage = new HttpRequestMessage(HttpMethod.Delete, endpoint))
{
...
HttpResponseMessage response = await _client.SendAsync(requestMessage);
.....
1
vote
0
answers
47
views
A "using" statement gets moved automatically outside of the namespace
A file within a custom VS project template is getting altered automatically by VS when the template is used to create a project.
Code in the template zip file:
using Enterprise.Utilities.ServiceBase....
0
votes
1
answer
95
views
is it necessary to use using Dispose the connection , open and close connection if using DbContext dapper in MS access database on vb.net
is it necessary to use using Dispose the connection, open and close connection if using DbContext dapper in MS access database on vb.net.
This is my first time using dapper, please guide me so that ...
-1
votes
1
answer
115
views
C# setting members from using statement
class A
{
TypeX PropertyX;
void SomeMethod()
{
using (DisposableType y = new DisposableType())
{
PropertyX = y.GetX();
}
}
}
What happens to PropertyX when Y is being ...
0
votes
1
answer
52
views
Does using statement close all kinds of streams in it?
I have using statement to close the OleDbConnection and then I'm working with the reader.
I'm wondering if I have to close the reader at the end or I can rely on the using statement to close it.
I've ...
-1
votes
3
answers
273
views
why we use nested using statement in c#?
using (StreamReader outFile = new StreamReader(outputFile.OpenRead()))
{
StreamReader resFile = new StreamReader(resultFile.OpenRead())
{ //Some Codes }
}
Why does the above ...
0
votes
2
answers
194
views
C# What is the point of the using statement? [duplicate]
I'm not talking about references to assemblies, rather the using statement within the code.
For example what is the difference between this:
using (DataReader dr = .... )
{
...stuff involving data ...
1
vote
1
answer
653
views
Why is c# creating a file before any file stream is created?
Background:
I am creating an avaloniaUI project and in it I have a json config with a class that serializes and deserializes it on initialization and editing. If the file can't be found on ...
1
vote
1
answer
426
views
Syntax for using declaration without variable [duplicate]
Consider a disposable with side effects in the constructor:
public class MyDisposable : IDisposable
{
public MyDisposable()
{
// Some side effects
...
}
public void ...
0
votes
0
answers
286
views
Εrror when locking a local variable: "Possibly incorrect assignment to local which is the argument to a using or lock statement."
I have an in memory static repository which is used both for writing into it and reading from it. When writing (updating) that repository, I would like to make it completely exclusively available for ...
0
votes
0
answers
33
views
Using same disposable object twice in diffrent using-statment [duplicate]
I have made a disposable class UnitOfWOrk which will also acts as an optional parameter, I have provided an exampel below.
public async Task CallPersistItemPassUoW()
{
using (var unitOfWork = new ...
0
votes
1
answer
410
views
Why is the using variable disposing itself before leaving the method?
using PuppeteerSharp;
using System;
using System.Threading.Tasks;
namespace Video_Handler
{
public class VideoHost
{
private static bool Is_HTTPS_URL(string url)
{
...
5
votes
2
answers
2k
views
Why can't a using variable be used within a switch section without braces in C#?
Consider the following code:
switch ("")
{
case "":
using var s = new MemoryStream();
break;
}
The above code won't compile with this ...
3
votes
1
answer
344
views
Do I need to release a connection if I throw an error?
So if I get a connection pool, then something unexpected happens and the error will be thrown, do I need to additionally release a connection? What happens with the MySQL connection pool once the ...
0
votes
1
answer
1k
views
How can I fix the error on Newtonsoft.Json using statement?
I am working on a .Net console app with c# and I have just added the Newtonsoft.Json using statement but VS code points it out as an error
The type or namespace name 'Newtonsoft' could not be found (...
0
votes
1
answer
238
views
IDisposable, using and GarbageColleciton [duplicate]
I got stuck in the weeds of how IDisposable and the GarbageCollector work.
Suppose you have an IDisposable object, which doesn't actually have an resources that it's holding onto (but the Dispose() ...
0
votes
1
answer
813
views
Declare a resource outside the using statement
I have a question about C# using statement. According to the docs, it is a best practice to both declare and initialize the resource (for instance, a SqlConnection object) inside the using statement. ...
2
votes
2
answers
142
views
C# - Is there a reason NOT to instantiate a class and use it in a single line of code?
I know this would be a correct way to use a StreamWriter:
using (StreamWriter sw = new StreamWriter(hreq.GetRequestStream()))
sw.Write(jsonPostData);
But what about this? Is this also valid or to ...
0
votes
0
answers
96
views
C# Get classes that has a reference to a specific type
In my project I would like to know the references of a specific type within my C# project. For example I have the class EntityA.
I have two other classes ProcessA and ProcessB. ProcessA and ProcessB ...
10
votes
3
answers
2k
views
Refactoring: using statement without scope, when does the implicit `Dispose` call happen?
I was refactoring some the other day, I bumped into something like that:
public async Task<Result> Handle(CancelInitiatedCashoutCommand command, CancellationToken cancellationToken)
{
using ...
0
votes
0
answers
66
views
Invalid attempt to call FieldCount when reader is closed occurring after fix put in for connection pool issue
The site I maintain was built with ASP Webforms. I was not involved in building it. Since upgrading the server where it is hosted we started seeing a lot of the following error:
"Timeout expired. The ...
-3
votes
2
answers
288
views
C# Dispose resources on exceptions without using
I'm writing a class library which includes socket operations. I don't want to rely on the consumer to dispose resources after finishing or when an exception is thrown.
Normally I would use the "using"...
0
votes
2
answers
674
views
Multiple using statements with if condition
I need to read one file and write processed data into one or optionally two files.
How to organize the Using statements in this case?
Will something like this work?
if (condition)
using (var sw2 ...
2
votes
1
answer
50
views
Multiple using declarations throw 'IOException' but statements don't when writing files
using declarations were just introduced in C# 8.0 but they don't behave the same as using blocks, or so i think.
The following nested using block works fine:
using (var resource = Assembly....