C# 14 Extension members for static classes

Recently, C# introduced extension members in version 14.
Can extension methods be written for static classes like Path
, File
or Directory
?
In the previous proposal (roles and extensions), something like the code below was valid.
public implicit extension FileExtension for File
{
public static void WriteBytes(string path, ReadOnlySpan<byte> data) { }
//etc
}
Answer
Clear Answer:
No, in C# 14
you still can't write extension members for static classes like Path
, File
, or Directory
.
Read more: Read more
The new "Extension Members" just allow you to extend instance types (class
, struct
, interface
). Static types can't be the target of these extensions.
The example you mentioned (public implicit extension FileExtension
for File
) was part of an earlier prototype during the Roles and Extensions discussion.
That model was reduced and simplified. The current Extension Members design does not allow static class extensions.
Read more: Proposal Extension Members
Enjoyed this article?
Check out more content on our blog or follow us on social media.
Browse more articles