很难找到关于这四种方法实际做什么的简单解释,针对网络编程新手.人们通常只是声明他们认为在特定场景中关闭套接字的正确方法,而不是在每个步骤背后的背景中发生的事情.
通过示教一个人对鱼的理念去,你能解释的Shutdown,Disconnect,Close和Dispose方法呢?
我正在使用visual studio 2016 git集成.如果我右键单击源文件,我可以看到该文件的历史记录.同样,如果它是项目中的子文件夹,我可以看到该文件夹的历史记录.
但是,如果我在解决方案资源管理器中选择项目或解决方案,它只会向我显示.csproj或.sln文件的历史记录.
如何查看整个项目或解决方案的历史记录(即与项目本身或包含项目的解决方案文件夹对应的文件夹)?
我正在编写一个实现ICollection<T>和ICollection接口的类.
MSDN说这些有点不同.ICollection<T>.CopyTo采取T[]论证,而ICollection.CopyTo采取System.Array争论.抛出的异常之间也存在差异.
这是我对泛型方法的实现(我相信它的功能完全正常):
void ICollection<PlcParameter>.CopyTo(PlcParameter[] array, int arrayIndex)
{
if (array == null)
throw new ArgumentNullException("array");
if (arrayIndex < 0)
throw new ArgumentOutOfRangeException("arrayIndex");
if (array.Length - arrayIndex < Count)
throw new ArgumentException("Not enough elements after arrayIndex in the destination array.");
for (int i = 0; i < Count; ++i)
array[i + arrayIndex] = this[i];
}
Run Code Online (Sandbox Code Playgroud)
但是,该方法的非泛型版本让我感到困惑.首先,如何检查以下异常情况?
源ICollection的类型不能自动转换为目标数组的类型.
第二,有没有办法利用现有的通用实现来减少代码重复?
这是我的在制品实施:
void ICollection.CopyTo(Array array, int index) …Run Code Online (Sandbox Code Playgroud) 根据我的理解,JIT-ed代码在程序运行时永远不会从内存中释放出来.这是否意味着反复调用.Compile()表达式树会泄漏内存?
这意味着只在静态构造函数中编译表达式树或以其他方式缓存它们,这可能不那么简单.对?
是否有可能解构一个未从方法返回的元组,但是是一个out参数?我不确定我是正确表达自己还是使用正确的术语,所以这里有一些例子:
void OutMethod(out (int aNumber, string someText) output)
=> output = (15, "yo");
void Usage()
{
{
// Works, of course.
OutMethod(out var tuple);
// But *slightly* aesthetically unappealing to use.
var usage = $"{tuple.someText}: {tuple.aNumber}";
}
{
// Would be awesome, but doesn't work.
OutFunction(out var(number, text));
}
{
// Would be awesome too, but doesn't work.
OutFunction(out (var number, var text));
}
{
// This doesn't work either.
OutFunction((out var number, out var text));
}
{
// Not …Run Code Online (Sandbox Code Playgroud) 在C#中,volatile关键字分别确保读取和写入具有获取和释放语义.但是,是否有关于引入读取或写入的内容?
例如:
volatile Thing something;
volatile int aNumber;
void Method()
{
// Are these lines...
var local = something;
if (local != null)
local.DoThings();
// ...guaranteed not to be transformed into these by compiler, jitter or processor?
if (something != null)
something.DoThings(); // <-- Second read!
// Are these lines...
if (aNumber == 0)
aNumber = 1;
// ...guaranteed not to be transformed into these by compiler, jitter or processor?
var temp = aNumber;
if (temp == 0) …Run Code Online (Sandbox Code Playgroud) 备注说明IList.IsReadOnly如下:
只读集合不允许在创建集合后添加,删除或修改元素.
这是否意味着实现IList的自定义类无法在内部添加或删除元素,还是仅仅禁止用户使用接口mothods进行操作?
如果允许内部修改,这是否意味着期望IList具有IsReadOnlytrue而永远不会改变的代码本身就会被破坏?
如果不允许内部修改,这是否意味着无法编写IList可在内部更改的有效内容,但不允许用户修改它?
出于调试目的,我编写了这个小静态方法:
public static long CheckMemory(long maxMemorySizeBytes)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var usedMemoryBytes = Process.GetCurrentProcess().VirtualMemorySize64;
if (usedMemoryBytes > maxMemorySizeBytes)
Debugger.Break();
return usedMemoryBytes;
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,VirtualMemorySize64不断返回比Visual Studio Diagnostic Tools窗口显示的内存更多的内存,以及任务管理器显示的内容.对于我现在正在运行的具体示例,以下是数字:
为什么会出现如此大的差异,如何从应用程序本身内正确跟踪内存使用情况?
回到当我学习时foreach,我在某处读到:
foreach (var element in enumerable)
{
// do something with element
}
Run Code Online (Sandbox Code Playgroud)
基本上相当于:
using (var enumerator = enumerable.GetEnumerator())
{
while (enumerator.MoveNext())
{
var element = enumerator.Current;
// do something with element
}
}
Run Code Online (Sandbox Code Playgroud)
如果既没有IEnumerator也没有IEnumerator<T>实现,为什么这段代码甚至可以编译IDisposable?C# 语言规范似乎只using在IDisposable.
这样的using声明有什么作用?
std::thread因为析构函数调用,class本质上是异常不安全的std::terminate.
std::thread t( function );
// do some work
// (might throw!)
t.join();
Run Code Online (Sandbox Code Playgroud)
当然,你可以把所有东西放在构造之间和join()try-catch块之间,但如果你知道你想要加入或分离,无论发生什么,这都会变得乏味且容易出错.
所以我在想如何围绕它编写最简单的包装器,但这也会支持其他假设类型的线程.例如,boost::thread或者完全不同的东西,只要它有joinable(),join()和detach()方法.这是我有多远:
// handles threads safely
// Acts the same as the underlying thread type, except during destruction.
// If joinable, will call join (and block!) during destruction.
// Keep in mind that any exception handling will get delayed because of that;
// it needs to wait for the thread to finish its work …Run Code Online (Sandbox Code Playgroud)