简体   繁体   中英

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)

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?

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.

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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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