小编rel*_*dom的帖子

套接字的关闭,断开,关闭和处理究竟做了什么?

很难找到关于这四种方法实际做什么的简单解释,针对网络编程新手.人们通常只是声明他们认为在特定场景中关闭套接字的正确方法,而不是在每个步骤背后的背景中发生的事情.

通过示教一个人对鱼的理念去,你能解释的Shutdown,Disconnect,CloseDispose方法呢?

.net c# sockets network-programming winsock

18
推荐指数
1
解决办法
8437
查看次数

如何在visual studio git中显示文件夹的历史记录

我正在使用visual studio 2016 git集成.如果我右键单击源文件,我可以看到该文件的历史记录.同样,如果它是项目中的子文件夹,我可以看到该文件夹​​的历史记录.

但是,如果我在解决方案资源管理器中选择项目或解决方案,它只会向我显示.csproj或.sln文件的历史记录.

如何查看整个项目或解决方案的历史记录(即与项目本身或包含项目的解决方案文件夹对应的文件夹)?

git visual-studio-2015 visual-studio-2017

17
推荐指数
2
解决办法
4151
查看次数

如何实现ICollection.CopyTo方法?

我正在编写一个实现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)

.net c#

14
推荐指数
1
解决办法
4633
查看次数

编译表达式树是否泄漏?

根据我的理解,JIT-ed代码在程序运行时永远不会从内存中释放出来.这是否意味着反复调用.Compile()表达式树会泄漏内存?

这意味着只在静态构造函数中编译表达式树或以其他方式缓存它们,这可能不那么简单.对?

.net c# expression memory-leaks expression-trees

11
推荐指数
2
解决办法
691
查看次数

是否可以解构ValueTuple参数?

是否有可能解构一个未从方法返回的元组,但是是一个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# c#-7.0

11
推荐指数
2
解决办法
969
查看次数

volatile是否会阻止引入读取或写入?

在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)

c# volatile lock-free compiler-optimization language-lawyer

8
推荐指数
1
解决办法
183
查看次数

IsReadOnly是否禁止您在内部更改自定义IList对象?

备注说明IList.IsReadOnly如下:

只读集合不允许在创建集合后添加,删除或修改元素.

这是否意味着实现IList的自定义类无法在内部添加或删除元素,还是仅仅禁止用户使用接口mothods进行操作?

如果允许内部修改,这是否意味着期望IList具有IsReadOnlytrue而永远不会改变的代码本身就会被破坏?

如果不允许内部修改,这是否意味着无法编写IList可在内部更改的有效内容,但不允许用户修改它?

.net c#

7
推荐指数
1
解决办法
199
查看次数

监控应用程序内存使用情况的正确方法是什么?

出于调试目的,我编写了这个小静态方法:

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窗口显示的内存更多的内存,以及任务管理器显示的内容.对于我现在正在运行的具体示例,以下是数字:

  • 诊断工具:~250 MB
  • 任务管理器:~120 MB
  • VirtualMemorySize64:~1100 MB

为什么会出现如此大的差异,如何从应用程序本身内正确跟踪内存使用情况?

.net c# memory memory-leaks

7
推荐指数
1
解决办法
2445
查看次数

为什么 using 语句在 IEnumerator 上起作用,它有什么作用?

回到当我学习时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# 语言规范似乎只usingIDisposable.

这样的using声明有什么作用?

c# language-lawyer

5
推荐指数
1
解决办法
615
查看次数

使std :: thread异常安全的最简单方法

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)

c++ multithreading exception c++11

3
推荐指数
1
解决办法
866
查看次数