Array.Copy和Buffer.BlockCopy都做同样的事情,但是BlockCopy针对快速字节级原始数组复制,而是Copy通用实现.我的问题是 - 在什么情况下你应该使用BlockCopy?您是否应该在复制基本类型数组时随时使用它,或者只有在编写性能时才使用它?使用Buffer.BlockCopy结束有什么固有的危险Array.Copy吗?
我注意到这两个接口,以及几个相关的类,已经在.NET 4中添加了.它们对我来说似乎有点多余; 我已经阅读了几个关于它们的博客,但我仍然无法弄清楚它们在.NET 4之前解决了哪些棘手问题.
什么是使用IStructuralEquatable和IStructuralComparable?
当我在ArrayList关键字上按f12转到从vs2008生成的元数据时,我发现生成的类声明如下
public class ArrayList : IList, ICollection, IEnumerable, ICloneable
Run Code Online (Sandbox Code Playgroud)
我知道IList已经从ICollection和IEnumerable继承了,那么为什么ArrayList会冗余地从这些接口继承呢?
在Java中,匿名内部类可以引用其本地范围中的变量:
public class A {
public void method() {
final int i = 0;
doStuff(new Action() {
public void doAction() {
Console.printf(i); // or whatever
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是这是如何实际实现的?如何i进入匿名内部doAction实现,为什么必须这样做final?
我正在尝试使用返回对象的表达式树创建一个方法,但我无法弄清楚如何实际指定要返回的对象.我试过读过这个,但实际上似乎没有在任何地方指定返回值.
我已经完成了所有的任务和内容,但是如何指定从使用表达式树创建的方法返回的对象?
编辑:这些是v4表达式树,我试图创建的方法是这样的:
private object ReadStruct(BinaryReader reader) {
StructType obj = new StructType();
obj.Field1 = reader.ReadSomething();
obj.Field2 = reader.ReadSomething();
//...more...
return obj;
}
Run Code Online (Sandbox Code Playgroud) 我一直在阅读x86指令集扩展,它们在某些特定情况下似乎很有用(例如SSE3中的HADDPD - (Horizontal-Add-Packed-Double)).这些需要一个特定的寄存器布局,需要有意设置,或者从它之前的一系列指令中进行.像gcc这样的通用编译器多长时间实际使用这些指令(或其子集),还是主要用于手工编码的汇编程序?编译器如何检测使用SIMD指令的适当位置?
如果要对流中的整数值求和,有两种主要方法:
ToIntFunction<...> mapFunc = ...
int sum = stream().collect(Collectors.summingInt(mapFunc))
int sum = stream().mapToInt(mapFunc).sum()
Run Code Online (Sandbox Code Playgroud)
第一个涉及装箱返回的整数并将其拆箱,但第二个步骤涉及额外的步骤.
哪个更有效/更清晰?
要从方法创建委托,您可以使用compile type-safe语法:
private int Method() { ... }
// and create the delegate to Method...
Func<int> d = Method;
Run Code Online (Sandbox Code Playgroud)
属性是getter和setter方法的包装器,我想创建一个属性getter方法的委托.就像是
public int Prop { get; set; }
Func<int> d = Prop;
// or...
Func<int> d = Prop_get;
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用.我必须创建一个单独的lambda方法,当getter方法匹配委托签名时,这似乎是不必要的:
Func<int> d = () => Prop;
Run Code Online (Sandbox Code Playgroud)
为了直接使用委托方法,我必须使用讨厌的反射,这不是编译类型安全的:
// something like this, not tested...
MethodInfo m = GetType().GetProperty("Prop").GetGetMethod();
Func<int> d = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), m);
Run Code Online (Sandbox Code Playgroud)
有没有办法直接以编译安全的方式在属性获取方法上创建委托,类似于在顶部的普通方法上创建委托,而不需要使用中间lambda方法?
我正在尝试创建一个命名的互斥锁,但是当我调用构造函数时,我得到了一个DirectoryNotFoundException!为什么互斥锁试图访问文件系统,我怎么知道什么是有效路径?是否应该放置互斥锁的特定目录,以及它与名称的对应关系如何?
编辑:我正在使用Mutex(bool, string)重载,例外是:
System.IO.DirectoryNotFoundException: Could not find a part of the path '<mutex name>'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.Threading.Mutex.<>c__DisplayClass3.<.ctor>b__0(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name, Boolean& createdNew, MutexSecurity mutexSecurity)
at System.Threading.Mutex..ctor(Boolean initiallyOwned, String name)
Run Code Online (Sandbox Code Playgroud) 我想只在其他接口中实现某些接口,我不希望它们能够被类直接继承.
提前致谢!
.net ×4
c# ×3
inheritance ×2
interface ×2
java ×2
arraylist ×1
arrays ×1
assembly ×1
closures ×1
delegates ×1
equality ×1
gcc ×1
icomparable ×1
java-8 ×1
java-stream ×1
mutex ×1
properties ×1
sse ×1
sum ×1
x86 ×1