Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "How to create a new method for an enumeration"
description: Learn how to use extension methods to add functionality to an enum in C#. This example shows an extension method called Passing for an enum called Grades.
ms.date: 04/17/2025
ms.date: 11/25/2025
helpviewer_keywords:
- "enumerations [C#]"
- "extension methods [C#], for enums"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
---
title: "How to use implicitly typed local variables and arrays in a query expression"
description: Use implicitly typed local variables in C# to have the compiler determine the type of a local variable. You must use them to store anonymous types.
ms.date: 07/20/2015
ms.date: 11/25/2025
helpviewer_keywords:
- "implicitly-typed local variables [C#], how to use"
ms.topic: how-to
ms.assetid: 6b7354d2-af79-427a-b6a8-f74eb8fd0b91
---
# How to use implicitly typed local variables and arrays in a query expression (C# Programming Guide)

Expand All @@ -25,6 +24,6 @@ You can use implicitly typed local variables whenever you want the compiler to d

## See also

- [Extension Methods](./extension-methods.md)
- [Extension members](./extension-methods.md)
- [LINQ (Language-Integrated Query)](../../linq/index.md)
- [LINQ in C#](../../linq/index.md)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CustomExtensions;
using CustomExtensions;

string s = "The quick brown fox jumped over the lazy dog.";
// Call the method as if it were an
Expand All @@ -13,12 +13,14 @@ 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)
extension(string str)
{
return str.Split(new char[] {' ', '.','?'}, StringSplitOptions.RemoveEmptyEntries).Length;
// This is the extension member.
// The `str` parameter is declared on the extension declaration.
public int WordCount()
{
return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Constraints on type parameters"
description: Learn about constraints on type parameters. Constraints tell the compiler what capabilities a type argument must have.
ms.date: 10/10/2025
ms.date: 11/25/2025
f1_keywords:
- "defaultconstraint_CSharpKeyword"
- "notnull_CSharpKeyword"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,25 @@ unsafe public static byte[] ToByteArray<T>(this T argument) where T : unmanaged
// </Snippet15>

// <Snippet16>
public static TDelegate? TypeSafeCombine<TDelegate>(this TDelegate source, TDelegate target)
where TDelegate : System.Delegate
=> Delegate.Combine(source, target) as TDelegate;
extension<TDelegate>(TDelegate source) where TDelegate : System.Delegate
{
public TDelegate? TypeSafeCombine(TDelegate target)
=> Delegate.Combine(source, target) as TDelegate;
}
// </Snippet16>

// <Snippet18>
public static Dictionary<int, string> EnumNamedValues<T>() where T : System.Enum
extension<T>(T) where T : System.Enum
{
var result = new Dictionary<int, string>();
var values = Enum.GetValues(typeof(T));
public static Dictionary<int, string> EnumNamedValues()
{
var result = new Dictionary<int, string>();
var values = Enum.GetValues(typeof(T));

foreach (int item in values)
result.Add(item, Enum.GetName(typeof(T), item)!);
return result;
foreach (int item in values)
result.Add(item, Enum.GetName(typeof(T), item)!);
return result;
}
}
// </Snippet18>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
4 changes: 2 additions & 2 deletions docs/csharp/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -558,8 +558,6 @@ items:
href: programming-guide/classes-and-structs/implicitly-typed-local-variables.md
- name: "How to use implicitly typed local variables and arrays in a query expression"
href: programming-guide/classes-and-structs/how-to-use-implicitly-typed-local-variables-and-arrays-in-a-query-expression.md
- name: Extension Methods
href: programming-guide/classes-and-structs/extension-methods.md
- name: "How to implement and call a custom extension method"
href: programming-guide/classes-and-structs/how-to-implement-and-call-a-custom-extension-method.md
- name: "How to create a new method for an enumeration"
Expand All @@ -580,6 +578,8 @@ items:
href: programming-guide/classes-and-structs/static-constructors.md
- name: "How to write a copy constructor"
href: programming-guide/classes-and-structs/how-to-write-a-copy-constructor.md
- name: Extension members
href: programming-guide/classes-and-structs/extension-methods.md
- name: Finalizers
href: programming-guide/classes-and-structs/finalizers.md
- name: Object and Collection Initializers
Expand Down
175 changes: 175 additions & 0 deletions docs/csharp/tutorials/snippets/console-linq/InterimSteps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
namespace UnusedCode;

using LinqFaroShuffle;
internal class InterimSteps
{
// <StepOne>
static IEnumerable<string> Suits()
{
yield return "clubs";
yield return "diamonds";
yield return "hearts";
yield return "spades";
}

static IEnumerable<string> Ranks()
{
yield return "two";
yield return "three";
yield return "four";
yield return "five";
yield return "six";
yield return "seven";
yield return "eight";
yield return "nine";
yield return "ten";
yield return "jack";
yield return "queen";
yield return "king";
yield return "ace";
}
// </StepOne>

private static void InitialMain()
{
// <StepTwo>
var startingDeck = from s in Suits()
from r in Ranks()
select (Suit: s, Rank: r);

// Display each card that's generated and placed in startingDeck
foreach (var card in startingDeck)
{
Console.WriteLine(card);
}
// </StepTwo>

// <StepThree>
var top = startingDeck.Take(26);
var bottom = startingDeck.Skip(26);
// </StepThree>

}

private void StartShuffling()
{
var startingDeck = from s in Suits()
from r in Ranks()
select (Suit: s, Rank: r);

// Display each card that's generated and placed in startingDeck
foreach (var card in startingDeck)
{
Console.WriteLine(card);
}

var top = startingDeck.Take(26);
var bottom = startingDeck.Skip(26);

// <StepFive>
var shuffledDeck = top.InterleaveSequenceWith(bottom);

foreach (var c in shuffledDeck)
{
Console.WriteLine(c);
}
// </StepFive>
}

private void CompareSequences()
{
// <StepSix>
var startingDeck = from s in Suits()
from r in Ranks()
select (Suit: s, Rank: r);

// Display each card generated and placed in startingDeck in the console
foreach (var card in startingDeck)
{
Console.WriteLine(card);
}

var top = startingDeck.Take(26);
var bottom = startingDeck.Skip(26);

var shuffledDeck = top.InterleaveSequenceWith(bottom);

var times = 0;
// Re-use the shuffle variable from earlier, or you can make a new one
shuffledDeck = startingDeck;
do
{
shuffledDeck = shuffledDeck.Take(26).InterleaveSequenceWith(shuffledDeck.Skip(26));

foreach (var card in shuffledDeck)
{
Console.WriteLine(card);
}
Console.WriteLine();
times++;

} while (!startingDeck.SequenceEquals(shuffledDeck));

Console.WriteLine(times);
// </StepSix>
}

private void AddLogging()
{
// <StepSeven>
var startingDeck = (from s in Suits().LogQuery("Suit Generation")
from r in Ranks().LogQuery("Rank Generation")
select (Suit: s, Rank: r)).LogQuery("Starting Deck");

foreach (var c in startingDeck)
{
Console.WriteLine(c);
}

Console.WriteLine();
var times = 0;
var shuffle = startingDeck;

do
{
// Out shuffle
/*
shuffle = shuffle.Take(26)
.LogQuery("Top Half")
.InterleaveSequenceWith(shuffle.Skip(26)
.LogQuery("Bottom Half"))
.LogQuery("Shuffle");
*/

// In shuffle
shuffle = shuffle.Skip(26).LogQuery("Bottom Half")
.InterleaveSequenceWith(shuffle.Take(26).LogQuery("Top Half"))
.LogQuery("Shuffle");

foreach (var c in shuffle)
{
Console.WriteLine(c);
}

times++;
Console.WriteLine(times);
} while (!startingDeck.SequenceEquals(shuffle));

Console.WriteLine(times);
// </StepSeven>
}
}

// <StepFour>
public static class CardExtensions
{
extension<T>(IEnumerable<T> sequence)
{
public IEnumerable<T> InterleaveSequenceWith(IEnumerable<T> second)
{
// Your implementation goes here
return default;
}
}
}
// </StepFour>
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
Loading