Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 12, 2025

Summary

Code examples incorrectly documented that Clipboard.GetData() returns null when BinaryFormatter is unavailable. The actual behavior returns a NotSupportedException instance. Additionally, the code examples were checking for null instead of validating the returned type.

Changes

  • Updated C# and VB code comments in ObsoletePatterns.cs/vb to accurately reflect exception behavior
  • Changed "returns null" → "returns a NotSupportedException instance" (4 comment lines total)
  • Fixed conditional checks in C# from if (data != null) to if (data is Person person) using pattern matching
  • Fixed conditional checks in VB from If data IsNot Nothing Then to If TypeOf data Is Person Then
  • Updated VB cast from CType to DirectCast for better performance after type validation

Example

// Before:
// Later attempts to retrieve the data return null
object data = Clipboard.GetData("MyApp.Person");
if (data != null)
{
    Person person = (Person)data;
    Console.WriteLine($"Processing person: {person.Name}, Age: {person.Age}");
}

// After:
// Later attempts to retrieve the data return a NotSupportedException instance
object data = Clipboard.GetData("MyApp.Person");
if (data is Person person)
{
    Console.WriteLine($"Processing person: {person.Name}, Age: {person.Age}");
}

VB Example

' Before:
' Returns a NotSupportedException instance for a custom object type
If data IsNot Nothing Then
    Dim person As Person = CType(data, Person)
    Console.WriteLine($"Processing person: {person.Name}, Age: {person.Age}")
End If

' After:
' Returns a NotSupportedException instance for a custom object type
If TypeOf data Is Person Then
    Dim person As Person = DirectCast(data, Person)
    Console.WriteLine($"Processing person: {person.Name}, Age: {person.Age}")
End If

This aligns the code examples with the main article documentation, which correctly states that GetData() returns a NotSupportedException when BinaryFormatter is required but not enabled, and ensures the code properly validates the type before using the data.

Fixes #2146

Original prompt

This section details on the original issue you should resolve

<issue_title>Some code examples still show null for Clipboard GetData when instead they return an exception</issue_title>
<issue_description>### Type of issue

Code doesn't work

Description

There was a behavior change from the time the article was written to when it was actually published. Some of the code examples still show, expect, or comment about null being returned when in fact it returns a NotSupportedException instance. These should be fixed.

Page URL

https://learn.microsoft.com/en-us/dotnet/desktop/winforms/migration/clipboard-dataobject-net10

Content source URL

https://github.com/dotnet/docs-desktop/blob/main/dotnet-desktop-guide/winforms/migration/clipboard-dataobject-net10.md

Document Version Independent Id

d7829387-6c41-1e77-8c93-113d49382167

Platform Id

54424045-aea6-3a25-8c01-9c53dc5840ca

Article author

@adegeo

Metadata

  • ID: d7829387-6c41-1e77-8c93-113d49382167
  • PlatformId: 54424045-aea6-3a25-8c01-9c53dc5840ca
  • Service: dotnet-desktop
  • Sub-service: winforms

Related Issues</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…ad of null

Co-authored-by: adegeo <67293991+adegeo@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix clipboard code examples to handle NotSupportedException Fix clipboard GetData documentation to reflect NotSupportedException instead of null Nov 12, 2025
Copilot AI requested a review from adegeo November 12, 2025 22:35
Copilot finished work on behalf of adegeo November 12, 2025 22:35
@adegeo adegeo marked this pull request as ready for review November 13, 2025 18:02
Copilot AI review requested due to automatic review settings November 13, 2025 18:02
Copilot finished reviewing on behalf of adegeo November 13, 2025 18:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes documentation comments in code examples that incorrectly stated Clipboard.GetData() returns null when BinaryFormatter is unavailable. The actual behavior is that it returns a NotSupportedException instance.

  • Updated 4 comment lines across C# and VB code example files
  • Changes align with the main article documentation which correctly describes the exception return behavior
  • All modifications are limited to comments only; no code logic was changed

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
dotnet-desktop-guide/winforms/migration/snippets/clipboard-dataobject-net10/net/csharp/ObsoletePatterns.cs Updated 2 comments to reflect that GetData() returns a NotSupportedException instance instead of null
dotnet-desktop-guide/winforms/migration/snippets/clipboard-dataobject-net10/net/vb/ObsoletePatterns.vb Updated 2 comments to reflect that GetData() returns a NotSupportedException instance instead of null

Co-authored-by: adegeo <67293991+adegeo@users.noreply.github.com>
Copilot AI requested a review from adegeo November 17, 2025 19:44
Copilot finished work on behalf of adegeo November 17, 2025 19:44
Co-authored-by: adegeo <67293991+adegeo@users.noreply.github.com>
Copilot finished work on behalf of adegeo November 18, 2025 18:27
Copilot AI requested a review from adegeo November 18, 2025 18:27
@adegeo adegeo closed this Nov 18, 2025
@adegeo adegeo reopened this Nov 18, 2025
@adegeo adegeo enabled auto-merge (squash) November 20, 2025 21:30
@adegeo adegeo requested a review from BillWagner November 20, 2025 21:30
@adegeo
Copy link
Contributor

adegeo commented Nov 20, 2025

@BillWagner This is ready

@adegeo adegeo merged commit 7e0341d into main Nov 21, 2025
16 of 18 checks passed
@adegeo adegeo deleted the copilot/fix-clipboard-example-errors branch November 21, 2025 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some code examples still show null for Clipboard GetData when instead they return an exception

3 participants