喜欢 :
using ::size_t; using ::fpos_t; using ::FILE;
Run Code Online (Sandbox Code Playgroud)
事实上,这个问题受到了这个问题的评论的启发:
我有类似下面的代码...有人在这里提到WebClient,Stream和StreamReader对象都可以从使用块中受益.两个简单的问题:
1:这个小片段看起来如何使用块?我做自己的研究没有问题,所以资源链接很好但是看到一个例子会更快更容易,我会从中理解它.
2:我想养成良好的编码标准的习惯,如果我对使用积木更好的原因有所了解会有所帮助...是否只是让你不必担心关闭或在那里更多原因?谢谢!
WebClient client = new WebClient();
Stream stream = client.OpenRead(originGetterURL);
StreamReader reader = new StreamReader(stream);
JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
string encryptionKey = (string)jObject["key"];
string originURL = (string)jObject["origin_url"];
stream.Close()
reader.Close()
Run Code Online (Sandbox Code Playgroud) 我想知道Visual Studio,其他IDE以及任何其他类型的环境(即根本没有IDE)如何处理来自外部的代码.
起初我认为#includes是唯一的方法,通过将汇编文件放在Visual Studio汇编文件的指定目录中,然后使用<>格式将它们引入,或者将汇编文件放在项目中目录并使用""格式将它们带入(即分别为<>和"").但是现在我在这篇文章的最后给出了#using指令的例子(注意,它不同于'using'指令而没有'#',用于命名空间).另外,我在"配置属性"对话框中遇到了在visual studio中添加程序集引用.
那么,有人会让我直接了解在给定项目中添加汇编文件和其他代码的所有内容吗?
- 以下是让我困惑的例子 - >我的书中有这一部分说明:
"......该图将C++ 2008代码与传统C和本机C++代码结合在一起.它还提供了最常用于C++ 2008的两个程序集参考文件及其相关的命名空间.与使用Visual Studio开发时不同在编写单个源文件时,默认情况下不包含程序集引用文件.因此,必须为这些文件编写#using指令...."
#include <stdio.h>
#include <iostream>
#using <system.dll>
#using <system.windows.forms.dll>
// Associated namespace directives
using namespace std;
using namespace System;
using namespace System::Windows::Forms;
void main()
{
printf( "Hello, Earth\n"); // from stdio.h
cout << "Hello, Mars\n"; // from iostream
Console::WriteLine("Hello, Jupiter"); // from system.dll
MessageBox::Show ("Hello, Saturn"); // from system.windows.forms.dll
}
Run Code Online (Sandbox Code Playgroud) 我有一些代码正在执行以下操作,但我不明白using BaseTypeX::BaseTypeX这个代码实际上做了什么
.其余部分我理解,所以请不要解释模板专业化等.
template<typename TReturn, typename... TArgs>
class ClassX<TReturn(TArgs...)> : public Internal::ClassXImpl<TReturn, TArgs...> {
public:
using BaseTypeX = Internal::ClassXImpl<TReturn, TArgs...>;
using BaseTypeX::BaseTypeX; // what is this doing exactly?
inline ClassX() noexcept = default;
// member function
template<class TThis, class TFunc>
inline ClassX(TThis* aThis, TFunc aFunc) {
this->bind(aThis, aFunc); // note bind is implemented in the ClassXImpl class
}
Run Code Online (Sandbox Code Playgroud) 我写了一段代码。我想确保我以正确的方式处理对象。
我有一个像这样的一次性类,用于从非托管资源读取一些数据。
class MyFileReader : IDisposable
{
private readonly FileStream _stream;
public MyFileReader(FileStream stream)
{
_stream = stream;
}
public void Dispose()
{
_stream.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
目前在我的程序中,我像这样处理对象。
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (MyFileReader reader = new MyFileReader(stream))
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎没问题。后来我注意到类是通过引用传递的,所以也许如果我处理其中一个,就不需要处理另一个。
我的问题是我可以做这样的事情吗?
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
MyFileReader reader = new MyFileReader(stream);
// Remove IDisposable from MyFileReader and stream will close after using.
}
Run Code Online (Sandbox Code Playgroud)
还是这个?
FileStream stream = …Run Code Online (Sandbox Code Playgroud) 我遇到了一个奇怪的问题,我可以通过调用存储过程返回结果,但代码追溯失败.
public IEnumerable<T> ExecuteStoredProcedure<T>(string storedProcedureName, IDataMapper<T> mapper, IDictionary<string, object> parameters)
{
using (var connection = new SqlConnection(connectionString))
{
using (var cmd = new SqlCommand(storedProcedureName, connection))
{
cmd.CommandType = CommandType.StoredProcedure;
foreach (var key in parameters.Keys)
{
cmd.Parameters.AddWithValue(key, parameters[key]);
}
connection.Open();
SqlDataReader reader = cmd.ExecuteReader();
//return MapRecordsToDTOs(reader, mapper);
//let's test:
IEnumerable<T> result = MapRecordsToDTOs(reader, mapper);
var x = (new List<T>(result)).Count;
System.Diagnostics.Debug.WriteLine(x);
return result;
}
}
}
private static IEnumerable<T> MapRecordsToDTOs<T>(SqlDataReader reader, IDataMapper<T> mapper)
{
if (reader.HasRows)
{
while (reader.Read())
{
System.Diagnostics.Debug.WriteLine(reader["Id"]); //what's …Run Code Online (Sandbox Code Playgroud) 您能解释一下为什么此代码有效并打印16吗?
#include <iostream>
namespace X {
int p = 5;
}
namespace Y {
int p = 16;
using namespace X;
}
int main()
{
std::cout << Y::p;
}
Run Code Online (Sandbox Code Playgroud)
以及为什么这段代码会引发多重声明错误
#include <iostream>
namespace X {
int p = 5;
}
namespace Y {
int p = 16;
using X::p;
}
int main()
{
std::cout << Y::p;
}
Run Code Online (Sandbox Code Playgroud)
我听说using指令不仅仅是使用任何名称的声明的过程,因为它的工作方式似乎有所不同
但是我不明白为什么,你能给出一些详细的解释吗?
同样,这可以正常打印16,如果我仅使用X :: p的声明替换指令,则会抛出相同的错误
#include <iostream>
namespace X {
int p = 5;
}
int p = 16;
using namespace X;
int …Run Code Online (Sandbox Code Playgroud) 如果HttpClient的是不应该在使用使用声明,请参阅以下链接: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ http://www.nimaara.com/2016/11 /01/beware-of-the-net-httpclient/
因此,如果您不应该在 using 语句中使用 HttpClient ,那么为什么有这么多示例将 WebClient 放在 using 语句中(包括 Microsoft);我还没有看到一篇关于不在using语句中放置 WebClient 的文章?最终在最低级别 WebClient 和 HttpClient 最终在同一个地方打开一个 TCP/IP 套接字。是否介于两者之间使 WebClient 可以放入using语句?
注意我提出这个问题是有原因的:我有一个在 WinXP、Win7 完整系统和嵌入式系统上运行的客户端应用程序,我用来将 XML POST 到我的后端主机(CentOS 系统)的代码如下所示:
void PostXml(string url, string xml, int id) {
try
{
//Console.WriteLine("POST Request: " + xml);
using (WebClient wc = new WebClient())
{
wc.Proxy = null; //DEBUG: testing this to see if it helps webclient post failure after time x
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
Byte[] d …Run Code Online (Sandbox Code Playgroud) 我有一段代码,我想将using语句应用于commands可枚举中的每个命令。什么是 C# 语法?
await using var transaction = await conn.BeginTransactionAsync(cancel);
IEnumerable<DbCommand> commands = BuildSnowflakeCommands(conn, tenantId);
var commandTasks = new List<Task>();
foreach (var command in commands)
{
command.CommandTimeout = commandTimeout;
command.Transaction = transaction;
commandTasks.Add(command.ExecuteNonQueryAsync(cancel));
}
try
{
await Task.WhenAll(commandTasks);
}
catch (SnowflakeDbException)
{
await transaction.RollbackAsync(cancel);
return;
}
await transaction.CommitAsync(cancel);
Run Code Online (Sandbox Code Playgroud)
编辑:Rider / ReSharper 似乎认为这两个是等价的,或者至少我得到了将 the 转换for为 a的提示foreach(这显然是错误的):
for (var i = 0; i < commands.Count; i++)
{
await using var command = …Run Code Online (Sandbox Code Playgroud) 我的源代码文件中有一个别名,如下所示:
MyClass.cs
using System;
using SomeClass = NewClass;
public class Program
{
public static void Main()
{
Console.WriteLine(nameof(SomeClass));
}
}
public class NewClass { }
Run Code Online (Sandbox Code Playgroud)
我需要这个,因为我的类的名称发生了变化,现在我需要同时为旧的和新的类结构进行编译。
当我运行该代码时,我得到了"SomeClass"但我预料到的"NewClass"。在这种情况下,为什么不nameof使用using-directive反映别名?
using ×10
c# ×6
c++ ×3
idisposable ×2
assemblies ×1
async-await ×1
c++-cli ×1
c++11 ×1
httpclient ×1
include ×1
nameof ×1
namespaces ×1
reference ×1
scope ×1
stream ×1
streamreader ×1
templates ×1
webclient ×1