草庐IT

ref_count

全部标签

c# - 如何使 Moq 忽略 ref 或 out 的参数

在RhinoMocks中,您可以将模拟作为一揽子声明告知IgnoreArguments。在Moq中,您似乎必须为每个参数指定It.IsAny()。但是,这不适用于ref和out参数。如何在需要最小化内部服务调用以返回特定结果的地方测试以下方法:publicvoidMyMethod(){//DoStuffIListerrors=newList();varresult=_service.DoSomething(referrors,refparam1,param2);//Domorestuff}测试方法:publicvoidTestOfMyMethod(){//SetupvarmoqServ

c# - Array.Count() 比 List.Count() 慢得多

使用IEnumerable的扩展方法时Count(),数组至少比列表慢两倍。FunctionCount()List2,299int[]6,903差异从何而来?我知道两者都在调用CountICollection的属性(property):IfthetypeofsourceimplementsICollection,thatimplementationisusedtoobtainthecountofelements.Otherwise,thismethoddeterminesthecount.对于列表,它返回List.Count,对于数组,Array.Length.此外,Array.Len

c# - 在 C# 中将类作为 ref 参数传递并不总是按预期工作。谁能解释一下?

我一直以为类类型的方法参数默认是作为引用参数传递的。显然情况并非总是如此。在C#中考虑这些单元测试(使用MSTest)。[TestClass]publicclassSandbox{privateclassTestRefClass{publicintTestInt{get;set;}}privatevoidTestDefaultMethod(TestRefClasstestClass){testClass.TestInt=1;}privatevoidTestAssignmentMethod(TestRefClasstestClass){testClass=newTestRefClass(

c# - 数组如何实现 IList<T> 而不在 C# 中实现属性 "Count"?

这个问题在这里已经有了答案:HowdoarraysinC#partiallyimplementIList?(6个答案)关闭9年前。很长一段时间以来,我对以下内容感到好奇:int[]array=newint[1];intiArrayLength=array.Length;//1由于数组实现了IList接口(interface),因此允许:intiArrayCount=((IList)array).Count;//still1但是:intiArrayCount=array.Count;//Compileerror.WHY?intiArrayLength=array.Length;//Thi

c# - FakeItEasy 的 Out 和 Ref 参数

我有一个方法,它有一个返回多个记录的out参数。我想知道如何使用FakeItEasy模拟它。 最佳答案 您应该使用.AssignsOutAndRefParameters配置方法:[Test]publicvoidOutput_and_reference_parameters_can_be_configured(){varfake=A.Fake>();stringignored=null;A.CallTo(()=>fake.TryGetValue("test",outignored)).Returns(true).AssignsOutA

C#:对于空列表,Any() 与 Count()

Aquestion早先发布让我思考。Any()和Count()在空列表上使用时是否表现相似?如解释here,两者都应该经过GetEnumerator()/MoveNext()/Dispose()的相同步骤。我使用LINQPad上的快速程序对此进行了测试:staticvoidMain(){varlist=newList();Stopwatchstopwatch=newStopwatch();stopwatch.Start();for(inti=0;i一般结果似乎表明Count()在这种情况下更快。这是为什么?我不确定我的基准测试是否正确,如果不正确,我将不胜感激。编辑:我知道这在语义上更

elasticsearch - 更新 ElasticSearch docker 容器 Mac 主机的 max_map_count

我正在使用this在docker中启动elasticsearch的容器。根据manual我必须更新max_map_count才能启动容器sudosysctl-wvm.max_map_count=262144但是..我可以在启动它之后在我的主机(容器)中更新它,但我无法启动它。我做错了吗?ERROR:bootstrapchecksfailedmaxvirtualmemoryareasvm.max_map_count[65530]likelytoolow,increasetoatleast[262144]如果我尝试在我的主机(即Mac)上执行此操作,我会收到以下错误。sysctl:unkn

elasticsearch - 更新 ElasticSearch docker 容器 Mac 主机的 max_map_count

我正在使用this在docker中启动elasticsearch的容器。根据manual我必须更新max_map_count才能启动容器sudosysctl-wvm.max_map_count=262144但是..我可以在启动它之后在我的主机(容器)中更新它,但我无法启动它。我做错了吗?ERROR:bootstrapchecksfailedmaxvirtualmemoryareasvm.max_map_count[65530]likelytoolow,increasetoatleast[262144]如果我尝试在我的主机(即Mac)上执行此操作,我会收到以下错误。sysctl:unkn

c# - 无法在扩展方法中对第一个 ("this") 参数使用 ref 和 out?

为什么禁止使用ref修饰符调用ExtensionMethod?这是可能的:publicstaticvoidChange(refTestClasstestClass,TestClasstestClass2){testClass=testClass2;}而这个不是:publicstaticvoidChangeWithExtensionMethod(thisrefTestClasstestClass,TestClasstestClass2){testClass=testClass2;}但为什么呢? 最佳答案 您必须明确指定ref和out。

c# - 是否可以将属性作为 "out"或 "ref"参数传递?

如果不能,我可以将属性作为“out”或“ref”参数传递吗?为什么不呢?例如Personp=newPerson();...publicvoidTest(outp.Name); 最佳答案 很抱歉回答很简短,但是不行,C#语言规范不允许这样做。查看此answer到另一个问题,看看当你尝试时会发生什么。它还说明了为什么您不应该将该属性设置为公共(public)字段来绕过限制。希望对你有帮助编辑:你问为什么?您将变量传递给out或ref参数,您实际上传递的是变量的地址(或内存中的位置)。在函数内部,编译器知道变量的实际位置,并获取值并将值写