以下循环(12332*324234)中的限制是计算一次还是每次循环运行?
for(int i=0; i<12332*324234;i++)
{
//Do something!
}
Run Code Online (Sandbox Code Playgroud) 在我的jQuery Mobile项目中,我使用以下代码:
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1">
Run Code Online (Sandbox Code Playgroud)
我正在获取Safari浏览器地址栏和导航页脚.我如何隐藏这些,以便我可以展示我的应用程序?
我有现有的代码,看起来类似于:
IEnumerable<SomeClass> GetStuff()
{
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(sql, conn)
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
SomeClass someClass = f(reader); // create instance based on returned row
yield return someClass;
}
}
}
Run Code Online (Sandbox Code Playgroud)
看来我可以通过使用获益reader.ReadAsync().但是,如果我只修改一行:
while (await reader.ReadAsync())
Run Code Online (Sandbox Code Playgroud)
编译器通知我await只能在标async有的方法中使用,并建议我修改方法签名为:
async Task<IEnumerable<SomeClass>> GetStuff()
Run Code Online (Sandbox Code Playgroud)
但是,这样做会导致GetStuff()无法使用,因为:
body
GetStuff()不能是迭代器块,因为Task<IEnumerable<SomeClass>>它不是迭代器接口类型.
我确信我错过了异步编程模型的关键概念.
问题:
ReadAsync()在我的迭代器中使用吗?怎么样?将pdb文件与商业应用程序一起重新分发是明智的吗?
有时候,我正在使用堆栈跟踪从已部署的应用程序中获取更详细的错误报告日志; 可以在不依赖这些文件的情况下实现此功能吗?
此外,这些文件包含多少原始源代码的提示?使用它对我的应用程序进行反向工程会更容易吗?
在C#,任何用户定义的struct自动是的一个子类System.Struct System.ValueType和System.Struct System.ValueType是的一个子类System.Object.
但是当我们为对象类型引用分配一些结构时,它会被装箱.例如:
struct A
{
public int i;
}
A a;
object obj = a; // boxing takes place here
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:如果A是后代System.Object,编译器不能将它上传到对象类型而不是装箱吗?
据我所知,out参数的唯一用途是调用者可以从单个方法调用中获取多个返回值.但我们也可以使用ref参数获取多个结果值!
那么有没有其他情况下out参数可以证明是有用的,而我们不能使用ref参数呢?
谢谢.
我是D的新手,我正在寻找一种好的方法来编写类似Haskell的类型类,例如D中的Functors,Monoids等.
这是在Tango或Phobos中实现的吗?
我听说过对某些属性启用编译时类型检查的特性.它们可以用于类型类吗?
我已经尝试了一些模板专业化并想出了这个:
// Monoid.d
// generic Monoid gets called when there is no instance of Monoid for Type T
class Monoid(T) {
pragma(msg, "Type is not a Monoid");
}
// Monoid instance for double
class Monoid(T:double) {
static T mzero() { return 0; }
static T mappend(T a, T b ) { return a + b;}
}
// Monoid instance for int
class Monoid(T:int) {
static T mzero() { return 0; }
static T mappend(T a, T b …Run Code Online (Sandbox Code Playgroud) 首先,抱歉模糊的问题标题.我无法想出更精确的一个.
鉴于以下类型:
{ TCommand : ICommand }
«interface» «interface» /
+-----------+ +----------------------/----+
| ICommand | | ICommandHandler<TCommand> |
+-----------+ +---------------------------+
^ | Handle(command: TCommand) |
| +---------------------------+
| ^
| |
+------------+ +-------------------+
| FooCommand | | FooCommandHandler |
+------------+ +-------------------+
^
|
+-------------------+
| SpecialFooCommand |
+-------------------+
Run Code Online (Sandbox Code Playgroud)
我想编写一个Dispatch接受任何命令并将其发送到适当的方法ICommandHandler<>.我认为使用DI容器(Autofac)可能会大大简化从命令类型到命令处理程序的映射:
void Dispatch<TCommand>(TCommand command) where TCommand : ICommand
{
var handler = autofacContainer.Resolve<ICommandHandler<TCommand>>();
handler.Handle(command);
}
Run Code Online (Sandbox Code Playgroud)
假设DI容器知道上面显示的所有类型.现在我打电话给:
Dispatch(new SpecialFooCommand(…));
Run Code Online (Sandbox Code Playgroud)
实际上,这将导致Autofac抛出一个ComponentNotRegisteredException,因为没有ICommandHandler<SpecialFooCommand>可用的.
然而,理想情况下,我仍然希望SpecialFooCommand由最接近匹配的命令处理程序处理,即.通过FooCommandHandler在上面的例子. …
我经常通过向它添加自引用("反身")类型参数约束来使一个简单的界面变得更加复杂.例如,我可能会这样:
interface ICloneable
{
ICloneable Clone();
}
class Sheep : ICloneable
{
ICloneable Clone() { … }
} //^^^^^^^^^^
Sheep dolly = new Sheep().Clone() as Sheep;
//^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
成:
interface ICloneable<TImpl> where TImpl : ICloneable<TImpl>
{
TImpl Clone();
}
class Sheep : ICloneable<Sheep>
{
Sheep Clone() { … }
} //^^^^^
Sheep dolly = new Sheep().Clone();
Run Code Online (Sandbox Code Playgroud)
主要优点:实现类型(例如Sheep)现在可以引用自身而不是其基类型,从而减少了对类型转换的需求(如最后一行代码所示).
虽然这非常好,但我也注意到这些类型参数约束并不直观,并且在更复杂的场景中变得非常难以理解.*)
问题:有没有人知道另一种C#代码模式可以实现相同的效果或类似的东西,但是更容易掌握?
*)此代码模式可能不直观且难以理解,例如以下方式:
声明
X<T> where T : X<T>似乎是递归的,人们可能想知道为什么编译器不会陷入无限循环,推理,"如果T是X<T>,那么X<T>实际上是一个 …
从C/C++迈出C#世界的第一步,细节上有点模糊.据我所知,类默认通过引用传递,但是例如.列出<string>,如:
void DoStuff(List<string> strs)
{
//do stuff with the list of strings
}
Run Code Online (Sandbox Code Playgroud)
和其他地方
List<string> sl = new List<string>();
//next fill list in a loop etc. and then do stuff with it:
DoStuff(sl);
Run Code Online (Sandbox Code Playgroud)
是SL在这种情况下按引用传递或者是副本上进行,这样我需要重新定义,如工人功能
void DoStuff(ref List<string> strs)实际上是对sl本身采取行动而不是副本?