Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarification needed? #44404

Open
bstordrup opened this issue Jan 16, 2025 · 2 comments · May be fixed by #44408
Open

Clarification needed? #44404

bstordrup opened this issue Jan 16, 2025 · 2 comments · May be fixed by #44408
Assignees
Labels
dotnet-csharp/svc fundamentals/subsvc help wanted Good for community contributors to help [up-for-grabs] in-pr This issue will be closed (fixed) by an active pull request.

Comments

@bstordrup
Copy link

bstordrup commented Jan 16, 2025

Type of issue

Other (describe below)

Description

The section about Optional arguments could in my opinion be improved with a description of how nullable types fit in with respect to whether they are optional or not.

The section specifically says that an argument is considered optional if it has a default value assigned in the parameter list. But one could argue that because of the nullability, a T? myArgument definition does implicitly have a default value being null or default(T).

Looking into the C# Language Specification does not enlight the issue either.

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/programming-guide/classes-and-structs/named-and-optional-arguments.md

Document Version Independent Id

6be20f15-e5eb-0778-5fbe-38326d8e8f00

Article author

@BillWagner

Metadata

  • ID: e335f6e5-7d90-bfab-f95d-ab8ef3ba41fc
  • Service: dotnet-csharp
  • Sub-service: fundamentals

Related Issues

@dotnet-policy-service dotnet-policy-service bot added the ⌚ Not Triaged Not triaged label Jan 16, 2025
@BillWagner BillWagner added help wanted Good for community contributors to help [up-for-grabs] and removed ⌚ Not Triaged Not triaged labels Jan 16, 2025
@BillWagner
Copy link
Member

BillWagner commented Jan 16, 2025

Hi @bstordrup

I can see the need for more clarification. Your question is using two different forms of "optional".

  • A parameter that's nullable (T?) is optional, meaning that the argument can be null: void M(T? paramThatCanBeOptional)
  • An optional parameter is one declared with a default value. Callers need not supply an argument: void M(T optionalParam = someConstantValue). Callers could use thing.M(), eliding the argument for the optional parameter.

Adding to the confusion, a parameter could be both forms of optional: void M(T? allTheOptionalMeanings=default)

We'll look at this to determine how we can make that more clear to readers I've also added the "help wanted" tag if anyone wants to submit a PR before we make our next update here.

@shethaadit
Copy link
Contributor

Hi @BillWagner, I will raise PR for this soon. Could you please assign this to myself?

@dotnet-policy-service dotnet-policy-service bot added the in-pr This issue will be closed (fixed) by an active pull request. label Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc fundamentals/subsvc help wanted Good for community contributors to help [up-for-grabs] in-pr This issue will be closed (fixed) by an active pull request.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants