You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add language reference and fundamentals for C# 14 extension members (#45654)
* Publish extensions specification
Publish the feature specification for the extension members feature.
* First draft of the language reference
* Further edits for extensions
Notes and code updates
* update samples on enums
* Finish all programming guide updates
* fix build warnings
* one more build warning
* Finish edits on extension members
* fix two build warnings
* respond to feedback.
"_csharplang/proposals/simple-lambda-parameters-with-modifiers.md": "This proposal allows lambda parameters to be declared with modifiers without requiring their type names. You can add modifiers like `ref` and `out` to lambda parameters without specifying their type.",
818
820
"_csharplang/proposals/partial-events-and-constructors.md": "This proposal allows partial events and constructors to be declared in partial classes. The event and constructor can be split across class declarations.",
819
821
"_csharplang/proposals/null-conditional-assignment.md": "This proposal allows the null conditional operator to be used for the destination of assignment expressions. This allows you to assign a value to a property or field only if the left side is not null.",
822
+
"_csharplang/proposals/extensions.md": "This proposal enables new kinds of extension members. These new extension members support extension properties, extension static members, including extension operators.",
820
823
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "Learn about any breaking changes since the initial release of C# 10 and included in C# 11",
821
824
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "Learn about any breaking changes since the initial release of C# 11 and included in C# 12",
822
825
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "Learn about any breaking changes since the initial release of C# 12 and included in C# 13",
Copy file name to clipboardExpand all lines: docs/core/whats-new/dotnet-10/overview.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,10 +81,10 @@ C# 14 introduces several new features and enhancements to improve developer prod
81
81
-**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.
82
82
-**Implicit span conversions**: Introduces first-class support for `Span<T>` and `ReadOnlySpan<T>` with new implicit conversions, enabling more natural programming with these types.
83
83
-**Modifiers on simple lambda parameters**: Allows parameter modifiers like `ref`, `in`, or `out` in lambda expressions without specifying parameter types.
84
-
-**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.
85
84
-**Partial events and constructors**: Adds support for partial instance constructors and partial events, complementing partial methods and properties introduced in C# 13.
86
85
-**Extension members**: Extension methods now support static methods, instance properties, and static properties through `extension` blocks, enabling more flexible and powerful extensions.
87
86
-**Null-conditional assignment**: Simplifies conditional assignments by allowing properties or fields to be updated only if the containing instance exists, using the `?.` operator.
87
+
-**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.
88
88
89
89
For more information, see [What's new in C# 14](../../../csharp/whats-new/csharp-14.md).
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/object-oriented/index.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "Classes, structs, and records"
3
3
description: Describes the use of classes, structures (structs), and records in C#.
4
-
ms.date: 03/23/2022
4
+
ms.date: 04/17/2025
5
5
helpviewer_keywords:
6
6
- "structs [C#], about structs"
7
7
- "records [C#], about records"
@@ -14,17 +14,17 @@ helpviewer_keywords:
14
14
---
15
15
# Overview of object oriented techniques in C\#
16
16
17
-
In C#, the definition of a type—a class, struct, or record—is like a blueprint that specifies what the type can do. An object is basically a block of memory that has been allocated and configured according to the blueprint. This article provides an overview of these blueprints and their features. The [next article in this series](objects.md) introduces objects.
17
+
In C#, the definition of a type—a class, struct, or record—is like a blueprint that specifies what the type can do. An object is basically a block of memory allocated and configured according to the blueprint. This article provides an overview of these blueprints and their features. The [next article in this series](objects.md) introduces objects.
18
18
19
19
## Encapsulation
20
20
21
-
*Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. A class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that aren't intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits. For more information, see the [Object-oriented programming](../tutorials/oop.md) tutorial.
21
+
*Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. A class or struct can specify how accessible each of its members is to code outside of the class or struct. Member not intended for consumers outside of the class or assembly are hidden to limit the potential for coding errors or malicious exploits. For more information, see the [Object-oriented programming](../tutorials/oop.md) tutorial.
22
22
23
23
## Members
24
24
25
25
The *members* of a type include all methods, fields, constants, properties, and events. In C#, there are no global variables or methods as there are in some other languages. Even a program's entry point, the `Main` method, must be declared within a class or struct (implicitly when you use [top-level statements](../program-structure/top-level-statements.md)).
26
26
27
-
The following list includes all the various kinds of members that may be declared in a class, struct, or record.
27
+
The following list includes all the various kinds of members that can be declared in a class, struct, or record.
28
28
29
29
- Fields
30
30
- Constants
@@ -56,7 +56,7 @@ The default accessibility is `private`.
56
56
57
57
Classes (but not structs) support the concept of inheritance. A class that derives from another class, called the *base class*, automatically contains all the public, protected, and internal members of the base class except its constructors and finalizers.
58
58
59
-
Classes may be declared as [abstract](../../language-reference/keywords/abstract.md), which means that one or more of their methods have no implementation. Although abstract classes cannot be instantiated directly, they can serve as base classes for other classes that provide the missing implementation. Classes can also be declared as [sealed](../../language-reference/keywords/sealed.md) to prevent other classes from inheriting from them.
59
+
Classes can be declared as [abstract](../../language-reference/keywords/abstract.md), which means that one or more of their methods have no implementation. Although abstract classes can't be instantiated directly, they can serve as base classes for other classes that provide the missing implementation. Classes can also be declared as [sealed](../../language-reference/keywords/sealed.md) to prevent other classes from inheriting from them.
60
60
61
61
For more information, see [Inheritance](./inheritance.md) and [Polymorphism](./polymorphism.md).
62
62
@@ -66,7 +66,7 @@ Classes, structs, and records can implement multiple interfaces. To implement fr
66
66
67
67
## Generic Types
68
68
69
-
Classes, structs, and records can be defined with one or more type parameters. Client code supplies the type when it creates an instance of the type. For example, the <xref:System.Collections.Generic.List%601> class in the <xref:System.Collections.Generic> namespace is defined with one type parameter. Client code creates an instance of a `List<string>` or `List<int>` to specify the type that the list will hold. For more information, see [Generics](../types/generics.md).
69
+
Classes, structs, and records can be defined with one or more type parameters. Client code supplies the type when it creates an instance of the type. For example, the <xref:System.Collections.Generic.List%601> class in the <xref:System.Collections.Generic> namespace is defined with one type parameter. Client code creates an instance of a `List<string>` or `List<int>` to specify the type that the list holds. For more information, see [Generics](../types/generics.md).
70
70
71
71
## Static Types
72
72
@@ -86,9 +86,9 @@ You can instantiate and initialize class or struct objects, and collections of o
86
86
87
87
## Anonymous Types
88
88
89
-
In situations where it isn't convenient or necessary to create a named class you use anonymous types. Anonymous types are defined by their named data members. For more information, see [Anonymous types](../types/anonymous-types.md).
89
+
In situations where it isn't convenient or necessary to create a named class you use anonymous types. Named data members define anonymous types. For more information, see [Anonymous types](../types/anonymous-types.md).
90
90
91
-
## Extension Methods
91
+
## Extension Members
92
92
93
93
You can "extend" a class without creating a derived class by creating a separate type. That type contains methods that can be called as if they belonged to the original type. For more information, see [Extension methods](../../programming-guide/classes-and-structs/extension-methods.md).
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/17/2025
5
5
f1_keywords:
6
6
- "base"
7
7
- "BaseClass.BaseClass"
8
8
- "base_CSharpKeyword"
9
9
helpviewer_keywords:
10
10
- "base keyword [C#]"
11
-
ms.assetid: 8b645dbe-1a33-49b8-8716-1c401f9a5ea5
12
11
---
13
-
# base (C# Reference)
12
+
# The base keyword
14
13
15
14
The `base` keyword is used to access members of the base class from within a derived class. Use it if you want to:
16
15
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.
19
17
- Specify which base-class constructor should be called when creating instances of the derived class.
20
18
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.
24
20
25
21
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.
26
22
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.
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/17/2025
5
+
f1_keywords:
6
+
- "extension_CSharpKeyword"
7
+
- "extension"
8
+
---
9
+
# Extension declaration (C# Reference)
10
+
11
+
Beginning with C# 14, top level, nongeneric `static class` declarations can use `extension` containers to declare *extension members*. Extension members are methods or properties and can appear to be instance or static members. Earlier versions of C# enable *extension methods* by adding `this` as a modifier to the first parameter of a static method declared in a top-level, nongeneric static class.
12
+
13
+
The `extension` block specifies the type and receiver for extension members. You can declare methods and properties inside the `extension` declaration. The following example declares a single extension block that defines an instance extension method and an instance property.
The `extension` defines the receiver: `sequence`, which is an `IEnumerable<int>`. The receiver type can be nongeneric, an open generic, or a closed generic type. The name `sequence` is in scope in every instance member declared in that extension. The extension method and property both access `sequence`.
18
+
19
+
Any of the extension members can be accessed as though they were members of the receiver type:
You can declare any number of members in a single container, as long as they share the same receiver. You can declare as many extension blocks in a single class as well. Different extensions don't need to declare the same type or name of receiver. The extension parameter doesn't need to include the parameter name if the only members are static:
> An extension doesn't introduce a *scope* for member declarations. All members declared in a single class, even if in multiple extensions, must have unique signatures. The generated signature includes the receiver type in its name for static members and the receiver parameter for extension instance members.
33
+
34
+
The following example shows an extension method using the `this` modifier:
Both forms of extension methods generate the same intermediate language (IL). Callers can't make a distinction between them. In fact, you can convert existing extension methods to the new member syntax without a breaking change. The formats are both binary and source compatible.
43
+
44
+
## Generic extension blocks
45
+
46
+
Where you specify the type parameters for an extension member declared in an extension block depends on where that type parameter is required:
47
+
48
+
- You add the type parameter to the `extension` declaration when the type parameter is used in the receiver.
49
+
- You add the type parameter to the member declaration when the type is distinct from any type parameter specified on the receiver.
50
+
- You can't specify the same type parameter in both locations.
51
+
52
+
The following example shows an extension block for `IEnumerable<T>` where two of the extension members require a second type parameter:
The members `Append` and `Prepend` specify the *extra* type parameter for the conversion. None of the members repeat the type parameter for the receiver.
57
+
58
+
The equivalent extension method declarations demonstrate how those type parameters are encoded:
0 commit comments