我知道这可能听起来很奇怪,但我甚至不知道如何在互联网上搜索这种语法,我也不确定究竟是什么意思.
所以我看了一些MoreLINQ代码然后我注意到了这个方法
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
return _(); IEnumerable<TSource> _()
{
var knownKeys = new HashSet<TKey>(comparer);
foreach (var element in source)
{
if (knownKeys.Add(keySelector(element)))
yield return element;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个奇怪的回报声明是什么?return _();?
有人可以给我一个解释,为什么我得到两个不同的数字,分别是.在图14和15中,作为以下代码的输出?
#include <stdio.h>
int main()
{
double Vmax = 2.9;
double Vmin = 1.4;
double step = 0.1;
double a =(Vmax-Vmin)/step;
int b = (Vmax-Vmin)/step;
int c = a;
printf("%d %d",b,c); // 14 15, why?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望在两种情况下都能获得15分,但似乎我缺少一些语言的基础知识.
我不确定它是否相关,但我在CodeBlocks中进行测试.但是,如果我在某些在线编译器中键入相同的代码行(例如这个),我会得到两个打印变量的答案为15.
在C++中我们有静态关键字,在循环中是这样的:
for(int x=0; x<10; x++)
{
for(int y=0; y<10; y++)
{
static int number_of_times = 0;
number_of_times++;
}
}
Run Code Online (Sandbox Code Playgroud)
这里静态number_of_times初始化一次.我怎么能在python 3.x中做同样的事情?
编辑:由于大多数人感到困惑,我想指出我给出的代码只是C++静态用法的一个例子.我真正的问题是我想在函数中只初始化一个时间变量,因为我不希望它是全局的(等等!)或默认参数..
所以今天我在尝试建立公司解决方案时遇到了一些有趣的问题,我想问你们,你们知道为什么会这样吗?我被告知它可能来自我的机器/视觉工作室,因为其他人没有同样的问题.
所以我们在项目中有一个方法A:
private static string RpcRoutingKeyNamingConvention(Type messageType, ITypeNameSerializer typeNameSerializer)
{
string queueName = typeNameSerializer.Serialize(messageType);
return messageType.GetAttribute<GlobalRPCRequest>() != null || AvailabilityZone == null
? queueName
: queueName + "_" + AvailabilityZone;
}
Run Code Online (Sandbox Code Playgroud)
在哪里GetAttribute<GlobalRPCRequest>()定义public static class ReflectionHelpers
public static TAttribute GetAttribute<TAttribute>(this Type type) where TAttribute : Attribute;
Run Code Online (Sandbox Code Playgroud)
然后我们有项目B有方法:
public static string GetAttribute(this XElement node, string name)
{
var xa = node.Attribute(name);
return xa != null ? xa.Value : "";
}
Run Code Online (Sandbox Code Playgroud)
我必须指出,我们B在项目中引用了项目A.现在发生的是,当我尝试构建时,我得到编译错误: …
我们有一个应用程序使用我们的供应商提供的 SDK 来轻松地与他们集成。此 SDK 连接到 AMQP 端点,并简单地向我们的消费者分发、缓存和转换消息。以前,这种集成是通过 HTTP 使用 XML 作为数据源的,而旧的集成有两种缓存 DataContext 的方法 - 每个 Web 请求和每个托管线程 ID。(1)
但是,现在我们不通过 HTTP 集成,而是通过 AMQP 集成,这对我们来说是透明的,因为 SDK 正在执行所有连接逻辑,我们只剩下定义我们的使用者,因此没有选项可以“根据 Web 请求”缓存 DataContext,因此只剩下每个托管线程 id。我实现了责任链模式,所以当我们收到更新时,它被放在一个处理程序管道中,该管道使用 DataContext 根据新的更新更新数据库。这是管道的调用方法的样子:
public Task Invoke(TInput entity)
{
object currentInputArgument = entity;
for (var i = 0; i < _pipeline.Count; ++i)
{
var action = _pipeline[i];
if (action.Method.ReturnType.IsSubclassOf(typeof(Task)))
{
if (action.Method.ReturnType.IsConstructedGenericType)
{
dynamic tmp = action.DynamicInvoke(currentInputArgument);
currentInputArgument = tmp.GetAwaiter().GetResult();
}
else
{
(action.DynamicInvoke(currentInputArgument) as Task).GetAwaiter().GetResult();
}
}
else
{
currentInputArgument = action.DynamicInvoke(currentInputArgument); …Run Code Online (Sandbox Code Playgroud) 我只是想了解字段初始化器.我遇到了错误 - 字段初始值设定项不能使用非静态字段,方法或道具.在寻找答案的同时,我发现了这篇文章.
对帖子来说,大部分投票的答案表明,在调用构造函数之前,"this"不存在.有谁知道为什么会这样?为什么在调用字段初始值设定项之前不能存在'this'?
在我看来,来自C/C++背景,'this'只是堆上分配的一块内存.并且必须存在才能为"this"的任何成员分配值.(因此它绝对存在于字段初始化器之前).
所以, first(FirstOrDefault(predicate)) 一个在性能方面更好1
我明白了,我也认为再调用一个方法应该稍微慢一点,这只是我的直觉。尽管如此,我还是决定运行一些基准测试来证明我的权利,而我却知之甚少。
这是我运行基准测试的结果:
BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
Intel Core i7-3630QM CPU 2.40GHz (Ivy Bridge), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.1.101
[Host] : .NET Core 3.1.1 (CoreCLR 4.700.19.60701, CoreFX 4.700.19.60801), X64 RyuJIT
Job-XMZTSC : .NET Framework 4.8 (4.8.4121.0), X64 RyuJIT
Runtime=.NET 4.7.2
Method N Mean Error StdDev Ratio RatioSD
WherePlusFirstOrDefaultArray 10000 31.44 us 0.288 us 0.270 us 0.40 0.00
FirstOrDefaultArray 10000 78.47 us 0.679 us 0.635 us 1.00 …Run Code Online (Sandbox Code Playgroud) 所以通常我们会有一些模型
public class ConnectionStrings
{
public string Sql { get; set; }
public string NoSql { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
那么我们有appsettings.json如下内容:
"ConnectionStrings": {
"Sql": "some connection string",
"NoSql": "some other connection string"
}
Run Code Online (Sandbox Code Playgroud)
然后我将模型绑定如下:
services.Configure<ConnectionStrings>(
options => Configuration.GetSection("ConnectionStrings").Bind(options));
Run Code Online (Sandbox Code Playgroud)
一切正常,但是我的模型可变性并不重要,因为它保存着重要的信息,而且所有配置都是静态信息之后,因此一旦读取,我的模型应该保持原样。还有其他更安全的方法吗?
我正在尝试获取未读电子邮件,然后将其标记为已读。
当我运行代码时出现此异常:
ServiceObjectPropertyException 未处理:
Microsoft.Exchange.WebServices.dll 中发生类型为“Microsoft.Exchange.WebServices.Data.ServiceObjectPropertyException”的未处理异常
当我尝试将 Exchange 邮件对象映射到业务模型对象时发生此错误。
这是映射方法:
class MailMapper
{
public static PhishingMail Map(EmailMessage OutlookMail)
{
//Map Exchange email object op Business model email object
PhishingMail readMail = new PhishingMail();
readMail.Subject = OutlookMail.Subject;
return readMail;
}
}
Run Code Online (Sandbox Code Playgroud)
这是应该将电子邮件标记为已读的代码。
public List<PhishingMail> GetEmails()
{
phishingMailList = new List<PhishingMail>();
FolderId InboxId = new FolderId(WellKnownFolderName.Inbox, "A*******m@i*****nl");
FindItemsResults<Item> findResults = service.FindItems(InboxId, new ItemView(100));
foreach (Item phishingmail in findResults.Items)
{
((EmailMessage)phishingmail).Load(new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.IsRead));
if (!((EmailMessage)phishingmail).IsRead)
{
((EmailMessage)phishingmail).IsRead = true;
((EmailMessage)phishingmail)
.Update(ConflictResolutionMode.AutoResolve);
}
PhishingMail …Run Code Online (Sandbox Code Playgroud) 当我无法声明const或var时,为什么可以声明变量named let.我知道我永远不会这样做,但如果有合理的解释,我只是好奇.所以我可以这样做:
var let = 5;
let x = 3;
x + let -> 8
Run Code Online (Sandbox Code Playgroud)
为什么这甚至可能?
c# ×6
.net ×4
asp.net-core ×1
benchmarking ×1
c ×1
c#-7.0 ×1
datacontext ×1
ecmascript-6 ×1
javascript ×1
linq ×1
performance ×1
python ×1
python-3.x ×1