我理解当你using在头文件中放入一个声明时可以遇到的麻烦,所以我不想这样做.相反,我试图将using(或a namespace foo =)放在类声明中,以减少头文件中的重复输入.不幸的是我遇到编译错误.似乎它将是一个有用的功能.
#ifndef FOO_H
#define FOO_H
// This include defines types in namespace gee::whiz::abc::def,
// such as the class Hello.
#include "file_from_another_namespace.h"
// using namespace gee::whiz::abc::def; // BAD!
namespace x {
namespace y {
namespace z {
struct Foo {
using namespace gee::whiz::abc::def; // Illegal.
namespace other = gee::whiz::abc::def; // Illegal.
// Foo(gee::whiz::abc::def::Hello &hello); // annoyingly long-winded
Foo(other::Hello &hello); // better
//...
};
} } } // end x::y::z namespace
#endif // FOO_H …Run Code Online (Sandbox Code Playgroud) 我知道Using语句可以处理正在创建的对象.就像我想做这样的事情:
Using(SqlConnection conn = new SqlConnection(connString))
{
//some code
//How to show the users if conn is not opened up or generated some kind of error?
}
Run Code Online (Sandbox Code Playgroud)
如果conn没有打开或产生某种错误,如何显示用户?
使用C++命名空间时,您更喜欢显式命名它们,如下所示:
std::cout << "Hello, world!\n";
Run Code Online (Sandbox Code Playgroud)
或者您更喜欢using namespace:
using namespace std;
cout << "Hello, world!\n";
Run Code Online (Sandbox Code Playgroud)
如果您更喜欢后者,您是否在文件或功能范围内声明您的使用?
我个人更喜欢明确地命名它们 - 它更多的是打字,但是当使用命名空间的混合(例如std和boost)时,我发现它更具可读性.
这个问题可能重复,但我找不到一个好的答案.简短而简单,需要我申报
using namespace std;
Run Code Online (Sandbox Code Playgroud)
在C++程序中?
假设我有以下代码:
private void UpdateDB(QuoteDataSet dataSet, Strint tableName)
{
using(SQLiteConnection conn = new SQLiteConnection(_connectionString))
{
conn.Open();
using (SQLiteTransaction transaction = conn.BeginTransaction())
{
using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM " + tableName, conn))
{
using (SQLiteDataAdapter sqliteAdapter = new SQLiteDataAdapter())
{
sqliteAdapter.Update(dataSet, tableName);
}
}
transaction.Commit();
}
}
}
Run Code Online (Sandbox Code Playgroud)
C#文档声明,通过using语句,范围内的对象将被处理,并且我已经看到了几个建议我们不需要使用try/finally子句的地方.
我通常用try/finally包围我的连接,并且我总是关闭finally子句中的连接.鉴于上述代码,如果存在异常,假设连接将被关闭是否合理?
如果我没记错的话,当我在using SqlConnection块内使用yield 时,我得到了运行时异常.
using (var connection = new SqlConnection(connectionString))
{
var command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
yield reader[0];
}
// Call Close when done reading.
reader.Close();
}
Run Code Online (Sandbox Code Playgroud)
当我用yieldList 替换这些问题时,我每次迭代都添加了项目.
在内部using StreamReader块时,我没有发生同样的问题
using (var streamReader = new StreamReader(fileName))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
yield return line;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有解释为什么例外事件发生在前一种情况而不是后者?这个建筑是否可取?
编辑为了得到我过去做的错误(早期处理)你应该调用下面的第一个方法:
IEnumerable<string> Read(string fileName)
{
using …Run Code Online (Sandbox Code Playgroud) 可以有人解释我以下命名空间用法之间的区别:
using namespace ::layer::module;
和
using namespace layer::module;
是什么原因导致了额外的::前layer?
这是我用来从数据库中获取数据的示例代码:在DAO层:
public IEnumerable<IDataRecord> GetDATA(ICommonSearchCriteriaDto commonSearchCriteriaDto)
{
using(DbContext)
{
DbDataReader reader = DbContext.GetReader("ABC_PACKAGE.GET_DATA", oracleParams.ToArray(), CommandType.StoredProcedure);
while (reader.Read())
{
yield return reader;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在BO层我调用上面的方法,如:
List<IGridDataDto> GridDataDtos = MapMultiple(_costDriversGraphDao.GetGraphData(commonSearchCriteriaDto)).ToList();
Run Code Online (Sandbox Code Playgroud)
在mapper层上MapMultiple方法的定义如下:
public IGridDataDto MapSingle(IDataRecord dataRecord)
{
return new GridDataDto
{
Code = Convert.ToString(dataRecord["Code"]),
Name = Convert.ToString(dataRecord["Name"]),
Type = Convert.ToString(dataRecord["Type"])
};
}
public IEnumerable<IGridDataDto> MapMultiple(IEnumerable<IDataRecord> dataRecords)
{
return dataRecords.Select(MapSingle);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码运行良好,但我想知道上述代码的两个问题.
所以我被困在这个问题大约一个星期.我试图运行一个项目来接收TCP连接并启动SignalR Hub作为服务.两者都完美地将项目作为.exe文件运行.TCP部分可以完美地工作,但是我遇到了SignalR方面的问题.
原因最终是使用声明.
之前
using (WebApp.Start<SignalrStartup>(url))
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Server running on {0}", url); // was url
Console.WriteLine("ID\tMessage");
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
后
WebApp.Start<SignalrStartup>(url);
Run Code Online (Sandbox Code Playgroud)
我曾尝试使用Console.WriteLine()注释掉的代码来运行代码,因为我认为它可能会抛出一个异常,因为没有控制台输出到一次作为服务运行.这也不起作用,但也不能作为.exe文件工作,因为它需要Console.ReadLine()保持控制台打开,排序你需要它来保持HelloWorld.cs打开.一旦使用的包装器与控制台一起被移除,它将在.exe和服务中工作.
我已经读过,一旦你离开包装器,using语句会杀死它中的对象.但我不明白After bit代码如何在运行时保持.exe代码打开.有没有使用任何点使用或曾经我一直在使用它错了吗?
编辑
protected override void OnStart(string[] args)
{
Task.Factory
.StartNew(() => StartTCP())
.ContinueWith(t => StartSignalR());
}
Run Code Online (Sandbox Code Playgroud)
该调用是从该StartSignalR()方法进行的.
using(...)语句是try {} finally {}的语法糖.
但是,如果我有一个如下所示的使用声明:
using (FileStream fs = File.Open(path))
{
}
Run Code Online (Sandbox Code Playgroud)
现在我想要捕获打开此文件可能导致的异常(这是相当高风险的代码,因为它可能因环境而失败),但是如果我在里面写try-catch会不会重复?当代码被编译为IL时,我假设当代码被JIT时,重复将被删除?
但是,我想要捕获打开文件可能导致的异常(所以我应该将try-catch包装在using语句的作用域之外),以及我在using块中做的任何异常,所以我应该添加try-catch在街区内.
这似乎是我在CLR可能在里面做了很多重复.CLR是否添加了catch子句?
我的同事认为使用声明是混乱的(但这是因为由于我需要对它们进行硬编码,所以单行很长,因为我需要非常快速地更改它们并且无法访问代码库的其他部分).说同事不使用using语句,但是using语句和try-finally/try-catch-finally之间是否存在任何功能差异?我确实看到了一个这样的案例,其中WCF服务有一个关于使用finally和返回值的一个鲜为人知的角落案例(最后的事情).解决方案是使用检查块.在C#中有这样的东西吗?
另一方面,是否所有类型都实现了非托管资源的IDisposale所有者?与我的朋友的讨论指出了不是的答案.(我也在本论坛的使用部分阅读了一些主题,其中有一些非常好的知识).
using ×10
c# ×6
c++ ×4
namespaces ×4
exception ×2
yield-return ×2
.net ×1
datareader ×1
header ×1
include ×1
linq ×1
signalr ×1