Skip to content

Commit b8fcd03

Browse files
committed
Finish programming guide.
1 parent 767c3c9 commit b8fcd03

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using CustomExtensions;
2+
3+
string s = "The quick brown fox jumped over the lazy dog.";
4+
// Call the method as if it were an
5+
// instance method on the type. Note that the first
6+
// parameter is not specified by the calling code.
7+
int i = s.WordCount();
8+
System.Console.WriteLine($"Word count of s is {i}");
9+
10+
11+
namespace CustomExtensions
12+
{
13+
// Extension methods must be defined in a static class.
14+
public static class StringExtension
15+
{
16+
extension(string str)
17+
{
18+
// This is the extension member.
19+
// The `str` parameter is declared on the extension declaration.
20+
public int WordCount()
21+
{
22+
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
23+
}
24+
}
25+
}
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

docs/csharp/programming-guide/generics/constraints-on-type-parameters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Constraints on type parameters"
33
description: Learn about constraints on type parameters. Constraints tell the compiler what capabilities a type argument must have.
4-
ms.date: 10/10/2025
4+
ms.date: 11/25/2025
55
f1_keywords:
66
- "defaultconstraint_CSharpKeyword"
77
- "notnull_CSharpKeyword"

docs/csharp/programming-guide/generics/snippets/GenericWhereConstraints.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,25 @@ unsafe public static byte[] ToByteArray<T>(this T argument) where T : unmanaged
175175
// </Snippet15>
176176

177177
// <Snippet16>
178-
public static TDelegate? TypeSafeCombine<TDelegate>(this TDelegate source, TDelegate target)
179-
where TDelegate : System.Delegate
180-
=> Delegate.Combine(source, target) as TDelegate;
178+
extension<TDelegate>(TDelegate source) where TDelegate : System.Delegate
179+
{
180+
public TDelegate? TypeSafeCombine(TDelegate target)
181+
=> Delegate.Combine(source, target) as TDelegate;
182+
}
181183
// </Snippet16>
182184

183185
// <Snippet18>
184-
public static Dictionary<int, string> EnumNamedValues<T>() where T : System.Enum
186+
extension<T>(T) where T : System.Enum
185187
{
186-
var result = new Dictionary<int, string>();
187-
var values = Enum.GetValues(typeof(T));
188+
public static Dictionary<int, string> EnumNamedValues()
189+
{
190+
var result = new Dictionary<int, string>();
191+
var values = Enum.GetValues(typeof(T));
188192

189-
foreach (int item in values)
190-
result.Add(item, Enum.GetName(typeof(T), item)!);
191-
return result;
193+
foreach (int item in values)
194+
result.Add(item, Enum.GetName(typeof(T), item)!);
195+
return result;
196+
}
192197
}
193198
// </Snippet18>
194199
}

docs/csharp/programming-guide/generics/snippets/generics.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
<ImplicitUsings>enable</ImplicitUsings>
88
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

0 commit comments

Comments
 (0)