在C#中丢弃的优点是什么 [英] What are the advantages of discards in c#

查看:203
本文介绍了在C#中丢弃的优点是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在C#中遇到了丢弃,并且想知道我感觉到的几件事

I've just come across discards in c# and am wondering a couple things that I feel Microsoft Docs on Discards didn't explain very well.

  1. 使用独立的丢弃程序仅仅是为了提高可读性吗?如果我有一个返回结果的函数(不一定需要返回结果),那么说一个 bool int ,但是还做了其他一些逻辑,那么为什么要使用丢弃什么时候可以简单地在赋值之外调用函数?
  1. With a standalone discard is it just meant for readability? If I had a function that returned a result (that isn't necessarily needed), let's say a bool or int, but also did some other logic then why use a discard when you can simply call the function outside of an assignment?

例如:

TcpClient client = new TcpClient();
NetworkStream nc = tcpClient.GetStream();
// what's the difference between
nc.Read(someBuffer, 0, someSize);
// and the discard
_ = nc.Read(someBuffer, 0, someSize);

他们不是都只是放弃结果了吗?看来这纯粹是我的偏爱.

Don't they both just discard the result anyways? This looks like it is purely a preference thing to me.

  1. 在Microsoft有关丢弃的文档中,有人说它会减少内存分配,因为它们等同于未分配的变量吗?
  2. 如果有的话,丢弃的性能优势是什么?这仅仅是一种旨在提高可读性的风格设计?

更明确地说:我只是想知道使用丢弃是否有任何潜在的性能优势.我提供的Microsoft doc链接指出只有一个丢弃变量,并且甚至可能没有为该变量分配存储,丢弃可以减少内存分配",在这种情况下(模式匹配,对方法的调用,out的调用,元组的解构和/或独立)是否减少了内存分配?

To be more clear: I'm just curious if there are any potential performance benefits to using discards. The Microsoft doc link that I provided states "there is only a single discard variable, and that variable may not even be allocated storage, discards can reduce memory allocations", in which use case (pattern matching, calls to methods with out, tuple deconstruction and/or standalone) are memory allocations reduced?

推荐答案

通常,在此句子中使用丢弃"是没有意义的:

In general, there is no sense to use discard in this sentence:

_ = nc.Read(someBuffer, 0, someSize);

因为您可以忽略返回值.我从未使用过任何独立的丢弃程序,但是文档涵盖了一些有用的特定情况.

since you can just ignore the return value. I did never anyone using standalone discards, but the documentation covers some specific cases when it can be useful.

当您必须提供变量但不需要它时,将使用丢弃.
如提供的文档页面中所述,丢弃对于以下内容非常有用:

The discards are used when you have to provide a variable, but don't need it.
As already covered in the provided documentation page, discards are very useful for:

switch (stream)
{
    case MemoryStream _:
    case FileStream _:
        ProcessStream(stream);
        break;
}

没有参数的方法

如果要检查是否可以将字符串解析为整数,但是不关心结果:

Methods with out parameters

If you want to check if you can parse a string to an integer, but don't care about the result:

if (int.TryParse(str, out _))
{
    // do something
}

否则,您将必须声明一个out变量,该变量将不使用并且会消耗一些内存.

Otherwise, you would have to declare an out variable, which you wouldn't use and which would consume some memory.

(_, _, area) = city.GetCityInformation(cityName);

如果没有丢弃,则将结果分解为3个不同的变量,并且仅使用其中一个.使用丢弃时,代码的意图更简洁,并且在内存管理方面更加无效.

Without discards you would deconstruct the result into 3 different variables, and use only one of them. With discards the code has cleaner intentions and more ineffective in terms of memory management.

这篇关于在C#中丢弃的优点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆