简体   繁体   English

需要解释为什么Clipboard.SetText(“”)不等于Clipboard.Clear()

[英]Need explanation why Clipboard.SetText(“”) is not equal to Clipboard.Clear()

I was passing by an article about the Clipboard.SetText() method from Microsoft: Clipboard.SetText Method (String) 我正在传递一篇关于Microsoft的Clipboard.SetText()方法的文章: Clipboard.SetText Method(String)

And I realize that if the string is empty(""), it would throw an exception. 我意识到如果字符串为空(“”),它将抛出异常。

Why would it be like that? 为什么会那样? Why it isn't equal to Clipboard.clear() instead? 为什么它不等于Clipboard.clear()呢?

I am just curious. 我只是好奇。

Like many methods it will be down to the implementation. 像许多方法一样,它将取决于实现。

In this case it is poorly implemented. 在这种情况下,它实施得很差。 I wouldn't personally think throwing a ArgumentNullException when supplying a value that isn't null is wise, as stated in your documentation link. 我个人认为在提供非null值时抛出ArgumentNullException是明智的,如文档链接中所述。

However, it makes no sense to store an empty string in the clipboard, so presumably the check was put in to block this from happening and make everyone's life easier. 但是,将空字符串存储在剪贴板中是没有意义的,因此可能会设置检查以阻止这种情况发生,并使每个人的生活更轻松。

Assuming this using the standard Windows API to call the clipboard, the actual implementation causes a different behaviour if the supplied value is NULL, in that it means the value will be accessed when you 'paste' or retrieve the contents. 假设这使用标准Windows API来调用剪贴板,如果提供的值为NULL,则实际实现会导致不同的行为,因为这意味着当您“粘贴”或检索内容时将访问该值。

As the clipboard can perform data conversions depending on the type specified in the clipboard, in theory there could be issues around having a blank string interpreted as NULL or vice versa. 由于剪贴板可以根据剪贴板中指定的类型执行数据转换,理论上可能存在将空字符串解释为NULL或反之亦然的问题。

This is speculation, because there isn't any detail on this in the documentation. 这是推测,因为文档中没有任何细节。 You'd have to test it to see what happens. 你必须测试它才能看到会发生什么。 Some interesting information is found in SetClipboardData function (MSDN). SetClipboardData函数 (MSDN)中提供了一些有趣的信息。

例如,考虑getDataObject的不同行为, null与保存空字符串的数据对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM