@@ -1364,6 +1364,31 @@ For a parameter `p`:
13641364
13651365When a parameter is annotated with `[UnscopedRef ]` ([§UnscopedRefAttribute ](attributes .md #unscopedrefattribute - the - unscopedref - attribute )), its ref - safe - context is widened by one level from its default : function - member becomes return - only , and return - only becomes caller - context .
13661366
1367+ > * Example * : The following illustrates how the implicit `this ` parameter of a struct instance method is `scoped ref ` (ref -safe -context of *function -member *), and how `[UnscopedRef ]` widens it to * return - only * , enabling ref returns of fields :
1368+ >
1369+ > < ! -- Example : {template : " standalone-lib-without-using" , name : " ParameterRefSafeContext" , expectedErrors : [" CS8170" ]} -- >
1370+ > ```csharp
1371+ > using System .Diagnostics .CodeAnalysis ;
1372+ >
1373+ > struct S
1374+ > {
1375+ > private int _field ;
1376+ >
1377+ > // Error: ref-safe-context of `this` is function-member,
1378+ > // so ref-safe-context of `_field` is also function-member,
1379+ > // which does not satisfy the return-only requirement.
1380+ > public ref int Bad () => ref _field ;
1381+ >
1382+ > // OK: [UnscopedRef] widens `this` from function-member to
1383+ > // return-only, so `_field` also has ref-safe-context of
1384+ > // return-only, satisfying the ref return requirement.
1385+ > [UnscopedRef ]
1386+ > public ref int Good () => ref _field ;
1387+ > }
1388+ > ```
1389+ >
1390+ > * end example *
1391+
13671392#### 9.7.2.4 Field ref safe context
13681393
13691394For a variable designating a reference to a field , `e .F `:
@@ -1481,3 +1506,29 @@ Any other difference with respect to `scoped` or `[UnscopedRef]` between the bas
14811506The `scoped ` modifier and `[UnscopedRef ]` attribute do not affect hiding .
14821507
14831508Overloads shall not differ only on `scoped ` or `[UnscopedRef ]`.
1509+
1510+ > *Example *: The following illustrates valid and invalid scope variance in overrides :
1511+ >
1512+ > < ! -- Example : {template : " standalone-lib-without-using" , name : " ParameterScopeVariance" , expectedErrors : [" CS0111" ]} -- >
1513+ > ```csharp
1514+ > using System ;
1515+ >
1516+ > class Base
1517+ > {
1518+ > public virtual void M (ref Span <int > x ) { }
1519+ > }
1520+ >
1521+ > class Derived : Base
1522+ > {
1523+ > // OK: adds scoped to a ref parameter
1524+ > public override void M (scoped ref Span <int > x ) { }
1525+ > }
1526+ >
1527+ > class C
1528+ > {
1529+ > void N (Span <int > x ) { }
1530+ > void N (scoped Span <int> x) { } // Error: overloads differ only on scoped
1531+ > }
1532+ > ```
1533+ >
1534+ > * end example *
0 commit comments