-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clarify extension method access (#42808)
* Clarify extension method declaration Extension methods can only be declared in top-level type. They can't be declared in nested types. * quick formatting fix * fix a snippet issue * one more time
- Loading branch information
1 parent
6673ac7
commit 3ce2e97
Showing
5 changed files
with
86 additions
and
94 deletions.
There are no files selected for viewing
51 changes: 21 additions & 30 deletions
51
...uide/classes-and-structs/how-to-implement-and-call-a-custom-extension-method.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...asses-and-structs/snippets/how-to-implement-and-call-a-custom-extension-method/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using CustomExtensions; | ||
|
||
string s = "The quick brown fox jumped over the lazy dog."; | ||
// Call the method as if it were an | ||
// instance method on the type. Note that the first | ||
// parameter is not specified by the calling code. | ||
int i = s.WordCount(); | ||
System.Console.WriteLine("Word count of s is {0}", i); | ||
|
||
|
||
namespace CustomExtensions | ||
{ | ||
// Extension methods must be defined in a static class. | ||
public static class StringExtension | ||
{ | ||
// This is the extension method. | ||
// The first parameter takes the "this" modifier | ||
// and specifies the type for which the method is defined. | ||
public static int WordCount(this string str) | ||
{ | ||
return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length; | ||
} | ||
} | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
...call-a-custom-extension-method/how-to-implement-and-call-a-custom-extension-method.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters