考虑以下代码段:
int i = 5 / 0;
Run Code Online (Sandbox Code Playgroud)
这给出了编译器错误CS0020:除以常数0,这很好。但是,下一个代码段:
int i = 10;
i = i / 0;
Run Code Online (Sandbox Code Playgroud)
编译就好了。
有人知道为什么吗?我看不出为什么编译器允许将整数变量除以零整数常量。
我有一个简单的程序,可以在2D点阵列中进行线性搜索.我对1000万个点进行1000次搜索.
奇怪的是,如果我生成1000个线程,程序的工作速度就像我只有我的CPU核心,或者当我使用Parallel.For时一样快.这与我所知道的关于创建线程的所有内容相反.创建和销毁线程很昂贵,但显然不是这种情况.
有人可以解释原因吗?
注意:这是一个方法论的例子; 搜索算法故意不意味着做到最优.重点是线程.
注2:我测试了4核i7和3核AMD,结果遵循相同的模式!
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
/// <summary>
/// We search for closest points.
/// For every point in array searchData, we search into inputData for the closest point,
/// and store it at the same position into array resultData;
/// </summary>
class Program
{
class Point
{
public double X { get; set; }
public double Y { get; set; }
public double GetDistanceFrom (Point p)
{
double dx, dy;
dx …Run Code Online (Sandbox Code Playgroud) 我有一个已编译的Azure函数,我想使用HttpTrigger属性定义自定义路由.
我的代码看起来像这样:
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "qotd/{format}")]HttpRequestMessage req, string format, TraceWriter log)
{
var quote = GetRandomQuote();
if (format == "json")
{
return req.CreateResponse(HttpStatusCode.OK, quote, "application/json");
}
else
{
var strQuote = quote.Text + Environment.NewLine + quote.Author;
return req.CreateResponse(HttpStatusCode.OK, strQuote, "text/plain");
}
}
Run Code Online (Sandbox Code Playgroud)
当我这样称呼它: localhost:7071/api/qotd/json 我得到404.
当我这样称它为: localhost:7071/api/qotd /?format = json 然后函数成功.
如果我这样称呼: localhost:7071/api/qotd / 我得到一个相当讨厌的错误:
"执行函数时出现异常:QotDFunction - >异常绑定参数'格式' - >绑定数据不包含预期值'format'......"
如何在HttpTrigger的Route参数中定义绑定,以便我可以像这样调用我的函数:
localhost:7071/api/qotd - 用于参数格式的默认值
和
localhost:7071/api/qotd/json - 将"json"作为格式的值传递?
对于路线,我也尝试了"qotd/{format:alpha?}"但得到了相同的结果.