Skip to content

Commit 7a70e26

Browse files
committed
Language reference- in progress
1 parent 76c00e0 commit 7a70e26

File tree

10 files changed

+206
-71
lines changed

10 files changed

+206
-71
lines changed

docs/core/whats-new/dotnet-10/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ C# 14 introduces several new features and enhancements to improve developer prod
7676
- **Unbound generic support for `nameof`**: The `nameof` expression now supports unbound generic types, such as `List<>`, returning the name of the type without requiring type arguments.
7777
- **Implicit span conversions**: Introduces first-class support for `Span<T>` and `ReadOnlySpan<T>` with new implicit conversions, enabling more natural programming with these types.
7878
- **Modifiers on simple lambda parameters**: Allows parameter modifiers like `ref`, `in`, or `out` in lambda expressions without specifying parameter types.
79-
- **Experimental feature - String literals in data section**: Enables emitting string literals as UTF-8 data into a separate section of the PE file, improving efficiency for certain scenarios.
8079
- **Partial events and constructors**: Adds support for partial instance constructors and partial events, complementing partial methods and properties introduced in C# 13.
8180
- **Extension members**: Extension methods now support static methods, instance properties, and static properties through `extension` blocks, enabling more flexible and powerful extensions.
8281
- **Null-conditional assignment**: Simplifies conditional assignments by allowing properties or fields to be updated only if the containing instance exists, using the `?.` operator.
82+
- **Experimental feature - String literals in data section**: Enables emitting string literals as UTF-8 data into a separate section of the PE file, improving efficiency for certain scenarios.
8383

8484
For more information, see [What's new in C# 14](../../../csharp/whats-new/csharp-14.md).
8585

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
11
---
2-
title: "base keyword"
2+
title: "The base keyword"
33
description: Learn about the base keyword, which is used to access members of the base class from within a derived class in C#.
4-
ms.date: 07/20/2015
4+
ms.date: 04/08/2025
55
f1_keywords:
66
- "base"
77
- "BaseClass.BaseClass"
88
- "base_CSharpKeyword"
99
helpviewer_keywords:
1010
- "base keyword [C#]"
11-
ms.assetid: 8b645dbe-1a33-49b8-8716-1c401f9a5ea5
1211
---
13-
# base (C# Reference)
12+
# The base keyword
1413

1514
The `base` keyword is used to access members of the base class from within a derived class. Use it if you want to:
1615

17-
- Call a method on the base class that has been overridden by another method.
18-
16+
- Call a method on the base class overridden by another method.
1917
- Specify which base-class constructor should be called when creating instances of the derived class.
2018

21-
The base class access is permitted only in a constructor, in an instance method, and in an instance property accessor.
22-
23-
Using the `base` keyword from within a static method will give an error.
19+
The base class access is permitted only in a constructor, in an instance method, and in an instance property accessor. Using the `base` keyword from within a static method produces an error.
2420

2521
The base class that is accessed is the base class specified in the class declaration. For example, if you specify `class ClassB : ClassA`, the members of ClassA are accessed from ClassB, regardless of the base class of ClassA.
2622

27-
## Example 1
28-
29-
In this example, both the base class `Person` and the derived class `Employee` have a method named `GetInfo`. By using the `base` keyword, it is possible to call the `GetInfo` method of the base class from within the derived class.
23+
In this example, both the base class `Person` and the derived class `Employee` have a method named `GetInfo`. By using the `base` keyword, it's possible to call the `GetInfo` method of the base class from within the derived class.
3024

31-
[!code-csharp[csrefKeywordsAccess#1](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsAccess/CS/csrefKeywordsAccess.cs#1)]
32-
33-
For additional examples, see [new](new-modifier.md), [virtual](virtual.md), and [override](override.md).
34-
35-
## Example 2
25+
:::code language="csharp" source="./snippets/csrefKeywordsAccess.cs" id="snippet1":::
3626

3727
This example shows how to specify the base-class constructor called when creating instances of a derived class.
3828

39-
[!code-csharp[csrefKeywordsAccess#2](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsAccess/CS/csrefKeywordsAccess.cs#2)]
29+
:::code language="csharp" source="./snippets/csrefKeywordsAccess.cs" id="snippet2":::
30+
31+
For more examples, see [new](new-modifier.md), [virtual](virtual.md), and [override](override.md).
4032

4133
## C# language specification
4234

@@ -45,4 +37,4 @@ This example shows how to specify the base-class constructor called when creatin
4537
## See also
4638

4739
- [C# Keywords](./index.md)
48-
- [this](./this.md)
40+
- [The `this` keyword](./this.md)
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
title: "extension member declarations"
3+
description: "Learn the syntax to declare extension members in C#. Extension members enable you to add functionality to types and interfaces in those instances where you don't have the source for the original type. Extensions are often paired with generic interfaces to implement a common set of functionality across all types that implement that interface."
4+
ms.date: 04/08/2025
5+
f1_keywords:
6+
- "extension_CSharpKeyword"
7+
- "extension"
8+
---
9+
# Extension declaration (C# Reference)
10+
11+
- Show new syntax with instance container
12+
- Show new syntax with static container
13+
- Show old syntax
14+
- Explain the lowering
15+
16+
Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type. For client code written in C#, F# and Visual Basic, there's no apparent difference between calling an extension method and the methods defined in a type.
17+
18+
The most common extension methods are the LINQ standard query operators that add query functionality to the existing <xref:System.Collections.IEnumerable?displayProperty=nameWithType> and <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType> types. To use the standard query operators, first bring them into scope with a `using System.Linq` directive. Then any type that implements <xref:System.Collections.Generic.IEnumerable%601> appears to have instance methods such as <xref:System.Linq.Enumerable.GroupBy%2A>, <xref:System.Linq.Enumerable.OrderBy%2A>, <xref:System.Linq.Enumerable.Average%2A>, and so on. You can see these additional methods in IntelliSense statement completion when you type "dot" after an instance of an <xref:System.Collections.Generic.IEnumerable%601> type such as <xref:System.Collections.Generic.List%601> or <xref:System.Array>.
19+
20+
### OrderBy Example
21+
22+
The following example shows how to call the standard query operator `OrderBy` method on an array of integers. The expression in parentheses is a lambda expression. Many standard query operators take lambda expressions as parameters, but this isn't a requirement for extension methods. For more information, see [Lambda Expressions](../../language-reference/operators/lambda-expressions.md).
23+
24+
[!code-csharp[csProgGuideExtensionMethods#3](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideExtensionMethods/cs/extensionmethods.cs#3)]
25+
26+
Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on. The parameter follows the [this](../../language-reference/keywords/this.md) modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a `using` directive.
27+
28+
The following example shows an extension method defined for the <xref:System.String?displayProperty=nameWithType> class. It's defined inside a non-nested, non-generic static class:
29+
30+
[!code-csharp[csProgGuideExtensionMethods#4](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideExtensionMethods/cs/extensionmethods.cs#4)]
31+
32+
The `WordCount` extension method can be brought into scope with this `using` directive:
33+
34+
```csharp
35+
using ExtensionMethods;
36+
```
37+
38+
And it can be called from an application by using this syntax:
39+
40+
```csharp
41+
string s = "Hello Extension Methods";
42+
int i = s.WordCount();
43+
```
44+
45+
You invoke the extension method in your code with instance method syntax. The intermediate language (IL) generated by the compiler translates your code into a call on the static method. The principle of encapsulation isn't really being violated. Extension methods can't access private variables in the type they're extending.
46+
47+
Both the `MyExtensions` class and the `WordCount` method are `static`, and it can be accessed like all other `static` members. The `WordCount` method can be invoked like other `static` methods as follows:
48+
49+
```csharp
50+
string s = "Hello Extension Methods";
51+
int i = MyExtensions.WordCount(s);
52+
```
53+
54+
The preceding C# code:
55+
56+
- Declares and assigns a new `string` named `s` with a value of `"Hello Extension Methods"`.
57+
- Calls `MyExtensions.WordCount` given argument `s`.
58+
59+
For more information, see [How to implement and call a custom extension method](./how-to-implement-and-call-a-custom-extension-method.md).
60+
61+
In general, you'll probably be calling extension methods far more often than implementing your own. Because extension methods are called by using instance method syntax, no special knowledge is required to use them from client code. To enable extension methods for a particular type, just add a `using` directive for the namespace in which the methods are defined. For example, to use the standard query operators, add this `using` directive to your code:
62+
63+
```csharp
64+
using System.Linq;
65+
```
66+
67+
(You may also have to add a reference to System.Core.dll.) You'll notice that the standard query operators now appear in IntelliSense as additional methods available for most <xref:System.Collections.Generic.IEnumerable%601> types.
68+
69+
## Binding Extension Methods at Compile Time
70+
71+
You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself. In other words, if a type has a method named `Process(int i)`, and you have an extension method with the same signature, the compiler will always bind to the instance method. When the compiler encounters a method invocation, it first looks for a match in the type's instance methods. If no match is found, it searches for any extension methods that are defined for the type, and bind to the first extension method that it finds.
72+
73+
## Example
74+
75+
The following example demonstrates the rules that the C# compiler follows in determining whether to bind a method call to an instance method on the type, or to an extension method. The static class `Extensions` contains extension methods defined for any type that implements `IMyInterface`. Classes `A`, `B`, and `C` all implement the interface.
76+
77+
The `MethodB` extension method is never called because its name and signature exactly match methods already implemented by the classes.
78+
79+
When the compiler can't find an instance method with a matching signature, it will bind to a matching extension method if one exists.
80+
81+
[!code-csharp[csProgGuideExtensionMethods#5](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideExtensionMethods/cs/extensionmethods.cs#5)]
82+
83+
## Common Usage Patterns
84+
85+
### Collection Functionality
86+
87+
In the past, it was common to create "Collection Classes" that implemented the <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType> interface for a given type and contained functionality that acted on collections of that type. While there's nothing wrong with creating this type of collection object, the same functionality can be achieved by using an extension on the <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType>. Extensions have the advantage of allowing the functionality to be called from any collection such as an <xref:System.Array?displayProperty=nameWithType> or <xref:System.Collections.Generic.List%601?displayProperty=nameWithType> that implements <xref:System.Collections.Generic.IEnumerable%601?displayProperty=nameWithType> on that type. An example of this using an Array of Int32 can be found [earlier in this article](#orderby-example).
88+
89+
### Layer-Specific Functionality
90+
91+
When using an Onion Architecture or other layered application design, it's common to have a set of Domain Entities or Data Transfer Objects that can be used to communicate across application boundaries. These objects generally contain no functionality, or only minimal functionality that applies to all layers of the application. Extension methods can be used to add functionality that is specific to each application layer without loading the object down with methods not needed or wanted in other layers.
92+
93+
```csharp
94+
public class DomainEntity
95+
{
96+
public int Id { get; set; }
97+
public string FirstName { get; set; }
98+
public string LastName { get; set; }
99+
}
100+
101+
static class DomainEntityExtensions
102+
{
103+
static string FullName(this DomainEntity value)
104+
=> $"{value.FirstName} {value.LastName}";
105+
}
106+
```
107+
108+
### Extending Predefined Types
109+
110+
Rather than creating new objects when reusable functionality needs to be created, we can often extend an existing type, such as a .NET or CLR type. As an example, if we don't use extension methods, we might create an `Engine` or `Query` class to do the work of executing a query on a SQL Server that may be called from multiple places in our code. However we can instead extend the <xref:System.Data.SqlClient.SqlConnection?displayProperty=nameWithType> class using extension methods to perform that query from anywhere we have a connection to a SQL Server. Other examples might be to add common functionality to the <xref:System.String?displayProperty=nameWithType> class, extend the data processing capabilities of the <xref:System.IO.Stream?displayProperty=nameWithType> object, and <xref:System.Exception?displayProperty=nameWithType> objects for specific error handling functionality. These types of use-cases are limited only by your imagination and good sense.
111+
112+
Extending predefined types can be difficult with `struct` types because they're passed by value to methods. That means any changes to the struct are made to a copy of the struct. Those changes aren't visible once the extension method exits. You can add the `ref` modifier to the first argument making it a `ref` extension method. The `ref` keyword can appear before or after the `this` keyword without any semantic differences. Adding the `ref` modifier indicates that the first argument is passed by reference. This enables you to write extension methods that change the state of the struct being extended (note that private members aren't accessible). Only value types or generic types constrained to struct (see [`struct` constraint](../../language-reference/builtin-types/struct.md#struct-constraint) for more information) are allowed as the first parameter of a `ref` extension method. The following example shows how to use a `ref` extension method to directly modify a built-in type without the need to reassign the result or pass it through a function with the `ref` keyword:
113+
114+
:::code language="csharp" source="./snippets/methods/Program.cs" id="Snippet9":::
115+
116+
This next example demonstrates `ref` extension methods for user-defined struct types:
117+
118+
:::code language="csharp" source="./snippets/methods/Program.cs" id="Snippet10":::
119+
120+
## General Guidelines
121+
122+
While it's still considered preferable to add functionality by modifying an object's code or deriving a new type whenever it's reasonable and possible to do so, extension methods have become a crucial option for creating reusable functionality throughout the .NET ecosystem. For those occasions when the original source isn't under your control, when a derived object is inappropriate or impossible, or when the functionality shouldn't be exposed beyond its applicable scope, Extension methods are an excellent choice.
123+
124+
For more information on derived types, see [Inheritance](../../fundamentals/object-oriented/inheritance.md).
125+
126+
When using an extension method to extend a type whose source code you aren't in control of, you run the risk that a change in the implementation of the type will cause your extension method to break.
127+
128+
If you do implement extension methods for a given type, remember the following points:
129+
130+
- An extension method is not called if it has the same signature as a method defined in the type.
131+
- Extension methods are brought into scope at the namespace level. For example, if you have multiple static classes that contain extension methods in a single namespace named `Extensions`, they'll all be brought into scope by the `using Extensions;` directive.
132+
133+
For a class library that you implemented, you shouldn't use extension methods to avoid incrementing the version number of an assembly. If you want to add significant functionality to a library for which you own the source code, follow the .NET guidelines for assembly versioning. For more information, see [Assembly Versioning](../../../standard/assembly/versioning.md).
134+
135+
## See also
136+
137+
- [Parallel Programming Samples (these include many example extension methods)](/samples/browse/?products=dotnet&term=parallel)
138+
- [Lambda Expressions](../../language-reference/operators/lambda-expressions.md)
139+
- [Standard Query Operators Overview](../../linq/standard-query-operators/index.md)
140+
- [Conversion rules for Instance parameters and their impact](/archive/blogs/sreekarc/conversion-rules-for-instance-parameters-and-their-impact)
141+
- [Extension methods Interoperability between languages](/archive/blogs/sreekarc/extension-methods-interoperability-between-languages)
142+
- [Extension methods and Curried Delegates](/archive/blogs/sreekarc/extension-methods-and-curried-delegates)
143+
- [Extension method Binding and Error reporting](/archive/blogs/sreekarc/extension-method-binding-and-error-reporting)
144+
145+
146+
## C# language specification
147+
148+
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
149+
150+
## See also
151+
152+
- [Extension methods](./this.md#extension-method-declarations)
153+
- <xref:System.Runtime.InteropServices.DllImportAttribute?displayProperty=nameWithType>
154+
- [C# Keywords](index.md)
155+
- [Modifiers](index.md)

docs/csharp/language-reference/keywords/index.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ A contextual keyword is used to provide a specific meaning in the code, but it i
123123
[`descending`](descending.md)
124124
[`dynamic`](../builtin-types/reference-types.md)
125125
[`equals`](equals.md)
126+
[`extension`](extension.md)
126127
:::column-end:::
127128
:::column:::
128129
[`field`](field.md)
@@ -137,9 +138,9 @@ A contextual keyword is used to provide a specific meaning in the code, but it i
137138
[`let`](let-clause.md)
138139
[`managed` (function pointer calling convention)](../unsafe-code.md#function-pointers)
139140
[`nameof`](../operators/nameof.md)
140-
[`nint`](../builtin-types/integral-numeric-types.md)
141141
:::column-end:::
142142
:::column:::
143+
[`nint`](../builtin-types/integral-numeric-types.md)
143144
[`not`](../operators/patterns.md#logical-patterns)
144145
[`notnull`](../../programming-guide/generics/constraints-on-type-parameters.md#notnull-constraint)
145146
[`nuint`](../builtin-types/integral-numeric-types.md)
@@ -151,9 +152,9 @@ A contextual keyword is used to provide a specific meaning in the code, but it i
151152
[`record`](../../fundamentals/types/records.md)
152153
[`remove`](remove.md)
153154
[`required`](required.md)
154-
[`scoped`](../statements/declarations.md#scoped-ref)
155155
:::column-end:::
156156
:::column:::
157+
[`scoped`](../statements/declarations.md#scoped-ref)
157158
[`select`](select-clause.md)
158159
[`set`](set.md)
159160
[`unmanaged` (function pointer calling convention)](../unsafe-code.md#function-pointers)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace keywords;
6+
internal class Extensions
7+
{
8+
}

0 commit comments

Comments
 (0)