Permalink
Switch branches/tags
AddLangServiceSatelliteAssemblies-180725 AddSatelliteAssembliesToSetup-180719 ArcadeTargets ArcadeTest DemoBranch DsrDependency Fix432432 FixRunSettings Layout MattGal-patch-1 PublishJson158159 RefactorToolsOptions VSSetupDrop bugfix/update-composition-package consolidateGoToDef dev/JieCarolHu/kaseyDemo531_bugFixed dev/JieCarolHu/kaseyDemo531 dev/brettfo/async-package dev/brettfo/sign-files dev/brettfo/snippet-syntax-facts dev/brettfo/version-packages dev/chborl/completestatement dev/chborley/completestatement dev/chborley/completestatement2 dev/chsienki/dipose_pattern_ext_method dev/cston/refs-only dev/features/sharwell-async-package dev/frsilb/disable-incremental-binding dev/hechang/FarRazor3 dev/hechang/OOPCompletion dev/hechang/RazorFar5 dev/hechang/mockup dev/hechang/newOptProf2 dev/hechang/optimization dev/hechang/projectapi dev/hechang/razorCleanup dev/hechang/removeOptimization dev/hechang/serialization2 dev/hechang/serialization4 dev/jaredpar/fix-del dev/jaredpar/fix-nuget dev/jaredpar/fix-official dev/jaredpar/fix-pack dev/jaredpar/fix-process dev/jaredpar/fix-publish dev/jaredpar/fix-queue dev/jaredpar/fix-sign dev/jaredpar/sdk dev/jaredpar/staging-queue dev/jaredpar/swix dev/jasonmal/delete-immutablehashmap dev/jcouv/update-docs dev/jinuj/PR-23448 dev/jorobich/new-classifications dev/olegtk/ExportFixes dev/olegtk/testNavBarIcons2 dev/sharwell/async-package dev/sharwell/vs-threading-analyzers dev/shgu/15.9p2version dev/shgu/15.9p2 dev/tmat/PublishData15.9 dev/tmat/ibcversionfix dev/tmat/3.0.0 dev15-rc-ri dev15-rc2 dev15-rc3 dev15.0.x dev15.1.x dev15.2.x dev15.3-preview1 dev15.3-preview2 dev15.3.x dev15.4.x dev15.5-preview1-staging dev15.5-preview1 dev15.5.x dev15.6-preview1-vs-deps dev15.6-preview2-vs-deps dev15.6-preview2 dev15.6-preview3-vs-deps dev15.6-preview3 dev15.6.x-vs-deps dev15.6.x dev15.7-preview1-vs-deps dev15.7-preview1-with-hotfixes-vs-deps dev15.7-preview1 dev15.7-preview3-vs-deps dev15.7-preview3 dev15.7.x-vs-deps dev15.7.x dev15.8-preview2-vs-deps dev15.8-preview2 dev15.8-preview3-vs-deps dev15.8-preview3 dev15.8-preview4-vs-deps dev15.8-preview4 dev15.8.x-vs-deps dev15.8.x dev15.9-preview1-vs-deps dev15.9-preview1 dev15.9-preview2-vs-deps dev15.9-preview2 dev15.9-preview3-vs-deps dev15.9-preview3 dev15.9.x-vs-deps dev15.9.x dev16.0.x-vs-deps dev16.0.x dpoeschl-patch-1 editorconfig-ide enableOOPByDefault fastInitialTaggingMaster feature/move-integration-tests-to-APEX features/AnnotatedTypes features/AsyncCompletion features/DefaultInterfaceImplementation features/EditorCompletion158_2 features/ExpressionVariables features/IVTCompletionTests features/NegatedConditionStatements features/NullableDogfood features/NullableReferenceTypes features/async-streams features/build-demo features/caller-expression features/ci features/codecov features/compiler features/continuation-comments features/custom-fixed features/dataflow features/decon-default features/editorconfig-in-compiler features/embeddedJson features/embeddedRegex features/enable-bonus-imports features/enhanced-using features/extensionEverything features/fixed-buffers features/generic-attributes features/invertif features/localsinit features/nested-stackalloc features/nullable-common features/optest features/range features/razor-far features/razorFar features/records features/recursive-patterns features/ref-partial features/ref-reassignment features/remotels features/sdk2 features/source-build features/source-generators features/strongname features/target-typed-new features/unmanaged-constructed-types features/verification features/xcopy findRefsOOPWork2RPS findRefsOOPWork2 fixsigning ide-dataflow-analyzer infrastructure/optprof-validation infrastructure/optprof inject-range-indexers insertion-snapshot/20181213.1 integ ivanbasov/asynccompletion master-vs-deps master microupdate netcore2.1-preview2 new-fixed-rules patterns2 releases/Dev14CTP5 releases/Dev14CTP6 releases/Dev14Preview releases/VS2015RC releases/VS2015 revert-30093-dev15.9.x-unused-crash rls sessionBGThread15.3 simpletagnostic sourceText sqliteWork5 tmeschter/AddSatelliteAssembliesToSetup-180719 update-readme updating-publish using-var vsts-dnceng-yml_increase-timeout
Nothing to show
Find file Copy path
f109e5d Jan 25, 2017
1 contributor

Users who have contributed to this file

executable file 41 lines (31 sloc) 1.76 KB

Discards

Discards are variables which you can assign to, but cannot read from. They don't have names. Instead, they are represented by an '_' (underscore). In C#7.0, they can appear in the following contexts:

  • out variable declarations, such as bool found = TryGetValue(out var _) or bool found = TryGetValue(out _)
  • deconstruction assignments, such as (x, _) = deconstructable;
  • deconstruction declarations, such as (var x, var _) = deconstructable;
  • is patterns, such as x is int _
  • switch/case patterns, such as case int _:

The principal representation of discards is an _ (underscore) designation in a declaration expression. For example, int _ in an out variable declaration or var (_, _, x) in a deconstruction declaration.

The second representation of discards is using the expression _ as a short-hand for var _, when no variable named _ is in scope. It is allowed in out vars, deconstruction assignments and declarations, and plain assignments (_ = IgnoredReturn();). It is, however, not allowed in C#7.0 patterns. When a variable named _ does exist in scope, then the expression _ is simply a reference to that variable, as it did in earlier versions of C#.

###Grammar changes

declaration_expression
	: type variable_designation
	;

variable_designation
	: single_variable_designation
	| parenthesized_variable_designation
	| discard_designation // new
	;

discard_designation // new
	: '_'
	;

References C# Language Design Notes for Oct 25 and 26, 2016 C# Design Notes for Oct 18, 2016 Design proposal for discards Note that those notes used to refer to discards as "wildcards".