-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10061 from dotnet/main
Merge main into live
- Loading branch information
Showing
190 changed files
with
6,662 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
|
||
> [!NOTE] | ||
> Some C# examples in this article run in the [Try.NET](https://try.dot.net) inline code runner and playground. Select the **Run** button to run an example in an interactive window. Once you execute the code, you can modify it and run the modified code by selecting **Run** again. The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages. | ||
> | ||
> The [local time zone](xref:System.TimeZoneInfo.Local) of the [Try.NET](https://try.dot.net) inline code runner and playground is Coordinated Universal Time, or UTC. This may affect the behavior and the output of examples that illustrate the <xref:System.DateTime>, <xref:System.DateTimeOffset>, and <xref:System.TimeZoneInfo> types and their members. | ||
> Some C# examples in this article run in the [Try.NET](https://github.com/dotnet/try) inline code runner and playground. Select **Run** to run an example in an interactive window. Once you execute the code, you can modify it and run the modified code by selecting **Run** again. The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages. | ||
> | ||
> The [local time zone](xref:System.TimeZoneInfo.Local) of the [Try.NET](https://github.com/dotnet/try) inline code runner and playground is Coordinated Universal Time, or UTC. This might affect the behavior and the output of examples that illustrate the <xref:System.DateTime>, <xref:System.DateTimeOffset>, and <xref:System.TimeZoneInfo> types and their members. |
10 changes: 10 additions & 0 deletions
10
snippets/fsharp/System.Collections.Generic/ComparerT/Overview/fs.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="program.fs" /> | ||
</ItemGroup> | ||
</Project> |
129 changes: 129 additions & 0 deletions
129
snippets/fsharp/System.Collections.Generic/ComparerT/Overview/program.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// <Snippet1> | ||
open System | ||
open System.Collections.Generic | ||
|
||
// <Snippet8> | ||
type Box(h, l, w) = | ||
member val Height: int = h with get | ||
member val Length: int = l with get | ||
member val Width: int = w with get | ||
|
||
interface IComparable<Box> with | ||
member this.CompareTo(other) = | ||
// Compares Height, Length, and Width. | ||
if this.Height.CompareTo other.Height <> 0 then | ||
this.Height.CompareTo other.Height | ||
elif this.Length.CompareTo other.Length <> 0 then | ||
this.Length.CompareTo other.Length | ||
elif this.Width.CompareTo other.Width <> 0 then | ||
this.Width.CompareTo other.Width | ||
else | ||
0 | ||
// </Snippet8> | ||
|
||
// <Snippet5> | ||
type BoxLengthFirst() = | ||
inherit Comparer<Box>() | ||
|
||
// <Snippet6> | ||
// Compares by Length, Height, and Width. | ||
override _.Compare(x: Box, y: Box) = | ||
if x.Length.CompareTo y.Length <> 0 then | ||
x.Length.CompareTo y.Length | ||
elif x.Height.CompareTo y.Height <> 0 then | ||
x.Height.CompareTo y.Height | ||
elif x.Width.CompareTo y.Width <> 0 then | ||
x.Width.CompareTo y.Width | ||
else | ||
0 | ||
// </Snippet6> | ||
// </Snippet5> | ||
|
||
// <Snippet2> | ||
let boxes = ResizeArray<Box>() | ||
boxes.Add(Box(4, 20, 14)) | ||
boxes.Add(Box(12, 12, 12)) | ||
boxes.Add(Box(8, 20, 10)) | ||
boxes.Add(Box(6, 10, 2)) | ||
boxes.Add(Box(2, 8, 4)) | ||
boxes.Add(Box(2, 6, 8)) | ||
boxes.Add(Box(4, 12, 20)) | ||
boxes.Add(Box(18, 10, 4)) | ||
boxes.Add(Box(24, 4, 18)) | ||
boxes.Add(Box(10, 4, 16)) | ||
boxes.Add(Box(10, 2, 10)) | ||
boxes.Add(Box(6, 18, 2)) | ||
boxes.Add(Box(8, 12, 4)) | ||
boxes.Add(Box(12, 10, 8)) | ||
boxes.Add(Box(14, 6, 6)) | ||
boxes.Add(Box(16, 6, 16)) | ||
boxes.Add(Box(2, 8, 12)) | ||
boxes.Add(Box(4, 24, 8)) | ||
boxes.Add(Box(8, 6, 20)) | ||
boxes.Add(Box(18, 18, 12)) | ||
|
||
// Sort by an Comparer<T> implementation that sorts | ||
// first by the length. | ||
boxes.Sort(BoxLengthFirst()) | ||
|
||
printfn "H - L - W" | ||
printfn "==========" | ||
|
||
for bx in boxes do | ||
printfn $"{bx.Height}\t{bx.Length}\t{bx.Width}" | ||
// </Snippet2> | ||
|
||
printfn "" | ||
printfn "H - L - W" | ||
printfn "==========" | ||
|
||
// <Snippet3> | ||
// Get the default comparer that | ||
// sorts first by the height. | ||
let defComp = Comparer<Box>.Default | ||
|
||
// Calling Boxes.Sort() with no parameter | ||
// is the same as calling boxes.Sort defComp | ||
// because they are both using the default comparer. | ||
boxes.Sort() | ||
|
||
for bx in boxes do | ||
printfn $"{bx.Height}\t{bx.Length}\t{bx.Width}" | ||
// </Snippet3> | ||
|
||
// <Snippet4> | ||
|
||
// This explicit interface implementation | ||
// compares first by the length. | ||
// Returns -1 because the length of BoxA | ||
// is less than the length of BoxB. | ||
let LengthFirst = BoxLengthFirst() | ||
|
||
let bc = LengthFirst :> Comparer<Box> | ||
|
||
let BoxA = Box(2, 6, 8) | ||
let BoxB = Box(10, 12, 14) | ||
let x = LengthFirst.Compare(BoxA, BoxB) | ||
printfn $"\n{x}" | ||
// </Snippet4> | ||
|
||
// <Snippet7> | ||
// This class is not demonstrated in the Main method | ||
// and is provided only to show how to implement | ||
// the interface. It is recommended to derive | ||
// from Comparer<T> instead of implementing IComparer<T>. | ||
type BoxComp() = | ||
interface IComparer<Box> with | ||
// Compares by Height, Length, and Width. | ||
member _.Compare(x: Box, y: Box) = | ||
if x.Height.CompareTo(y.Height) <> 0 then | ||
x.Height.CompareTo(y.Height) | ||
elif x.Length.CompareTo(y.Length) <> 0 then | ||
x.Length.CompareTo(y.Length) | ||
elif x.Width.CompareTo(y.Width) <> 0 then | ||
x.Width.CompareTo(y.Width) | ||
else | ||
0 | ||
// </Snippet7> | ||
|
||
// </Snippet1> |
14 changes: 14 additions & 0 deletions
14
snippets/fsharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/fs.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="source.fs" /> | ||
<Compile Include="source2.fs" /> | ||
<Compile Include="source3.fs" /> | ||
<Compile Include="source1.fs" /> | ||
<Compile Include="source4.fs" /> | ||
</ItemGroup> | ||
</Project> |
29 changes: 29 additions & 0 deletions
29
snippets/fsharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module source | ||
//<Snippet1> | ||
open System.Collections.Generic | ||
|
||
// Create a new sorted dictionary of strings, with string | ||
// keys. | ||
let openWith = SortedDictionary<string, string>() | ||
|
||
// Add some elements to the dictionary. | ||
openWith.Add("txt", "notepad.exe") | ||
openWith.Add("bmp", "paint.exe") | ||
openWith.Add("dib", "paint.exe") | ||
openWith.Add("rtf", "wordpad.exe") | ||
|
||
// Create a Dictionary of strings with string keys, and | ||
// initialize it with the contents of the sorted dictionary. | ||
let copy = Dictionary<string, string> openWith | ||
|
||
// List the contents of the copy. | ||
printfn "" | ||
|
||
for kvp in copy do | ||
printfn $"Key = {kvp.Key}, Value = {kvp.Value}" | ||
// This code example produces the following output: | ||
// Key = bmp, Value = paint.exe | ||
// Key = dib, Value = paint.exe | ||
// Key = rtf, Value = wordpad.exe | ||
// Key = txt, Value = notepad.exe | ||
//</Snippet1> |
33 changes: 33 additions & 0 deletions
33
snippets/fsharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source1.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module source1 | ||
//<Snippet1> | ||
open System | ||
open System.Collections.Generic | ||
|
||
// Create a new sorted dictionary of strings, with string | ||
// keys and a case-insensitive comparer. | ||
let openWith = | ||
SortedDictionary<string, string> StringComparer.CurrentCultureIgnoreCase | ||
|
||
// Add some elements to the dictionary. | ||
openWith.Add("txt", "notepad.exe") | ||
openWith.Add("Bmp", "paint.exe") | ||
openWith.Add("DIB", "paint.exe") | ||
openWith.Add("rtf", "wordpad.exe") | ||
|
||
// Create a Dictionary of strings with string keys and a | ||
// case-insensitive equality comparer, and initialize it | ||
// with the contents of the sorted dictionary. | ||
let copy = | ||
Dictionary<string, string>(openWith, StringComparer.CurrentCultureIgnoreCase) | ||
|
||
// List the contents of the copy. | ||
printfn "" | ||
|
||
for kvp in copy do | ||
printfn $"Key = {kvp.Key}, Value = {kvp.Value}" | ||
// This code example produces the following output: | ||
// Key = Bmp, Value = paint.exe | ||
// Key = DIB, Value = paint.exe | ||
// Key = rtf, Value = wordpad.exe | ||
// Key = txt, Value = notepad.exe | ||
//</Snippet1> |
36 changes: 36 additions & 0 deletions
36
snippets/fsharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source2.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module source2 | ||
//<Snippet1> | ||
open System | ||
open System.Collections.Generic | ||
|
||
// Create a new Dictionary of strings, with string keys | ||
// and a case-insensitive comparer for the current culture. | ||
let openWith = Dictionary<string, string> StringComparer.CurrentCultureIgnoreCase | ||
|
||
// Add some elements to the dictionary. | ||
openWith.Add("txt", "notepad.exe") | ||
openWith.Add("bmp", "paint.exe") | ||
openWith.Add("DIB", "paint.exe") | ||
openWith.Add("rtf", "wordpad.exe") | ||
|
||
// Try to add a fifth element with a key that is the same | ||
// except for case; this would be allowed with the default | ||
// comparer. | ||
try | ||
openWith.Add("BMP", "paint.exe") | ||
with :? ArgumentException -> | ||
printfn "\nBMP is already in the dictionary." | ||
|
||
// List the contents of the sorted dictionary. | ||
printfn "" | ||
|
||
for kvp in openWith do | ||
printfn $"Key = {kvp.Key}, Value = {kvp.Value}" | ||
// This code example produces the following output: | ||
// BMP is already in the dictionary. | ||
// | ||
// Key = txt, Value = notepad.exe | ||
// Key = bmp, Value = paint.exe | ||
// Key = DIB, Value = paint.exe | ||
// Key = rtf, Value = wordpad.exe | ||
//</Snippet1> |
25 changes: 25 additions & 0 deletions
25
snippets/fsharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source3.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module source3 | ||
//<Snippet1> | ||
open System.Collections.Generic | ||
|
||
// Create a new dictionary of strings, with string keys and | ||
// an initial capacity of 4. | ||
let openWith = Dictionary<string, string> 4 | ||
|
||
// Add 4 elements to the dictionary. | ||
openWith.Add("txt", "notepad.exe") | ||
openWith.Add("bmp", "paint.exe") | ||
openWith.Add("dib", "paint.exe") | ||
openWith.Add("rtf", "wordpad.exe") | ||
|
||
// List the contents of the dictionary. | ||
printfn "" | ||
|
||
for kvp in openWith do | ||
printfn $"Key = {kvp.Key}, Value = {kvp.Value}" | ||
// This code example produces the following output: | ||
// Key = txt, Value = notepad.exe | ||
// Key = bmp, Value = paint.exe | ||
// Key = dib, Value = paint.exe | ||
// Key = rtf, Value = wordpad.exe | ||
//</Snippet1> |
38 changes: 38 additions & 0 deletions
38
snippets/fsharp/System.Collections.Generic/DictionaryTKey,TValue/.ctor/source4.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module source4 | ||
//<Snippet1> | ||
open System | ||
open System.Collections.Generic | ||
|
||
// Create a new dictionary of strings, with string keys, an | ||
// initial capacity of 5, and a case-insensitive equality | ||
// comparer. | ||
let openWith = | ||
Dictionary<string, string>(5, StringComparer.CurrentCultureIgnoreCase) | ||
|
||
// Add 4 elements to the dictionary. | ||
openWith.Add("txt", "notepad.exe") | ||
openWith.Add("bmp", "paint.exe") | ||
openWith.Add("DIB", "paint.exe") | ||
openWith.Add("rtf", "wordpad.exe") | ||
|
||
// Try to add a fifth element with a key that is the same | ||
// except for case; this would be allowed with the default | ||
// comparer. | ||
try | ||
openWith.Add("BMP", "paint.exe") | ||
with :? ArgumentException -> | ||
printfn "\nBMP is already in the dictionary." | ||
|
||
// List the contents of the dictionary. | ||
printfn "" | ||
|
||
for kvp in openWith do | ||
printfn $"Key = {kvp.Key}, Value = {kvp.Value}" | ||
// This code example produces the following output: | ||
// BMP is already in the dictionary. | ||
// | ||
// Key = txt, Value = notepad.exe | ||
// Key = bmp, Value = paint.exe | ||
// Key = DIB, Value = paint.exe | ||
// Key = rtf, Value = wordpad.exe | ||
//</Snippet1> |
11 changes: 11 additions & 0 deletions
11
snippets/fsharp/System.Collections.Generic/DictionaryTKey,TValue/Overview/fs.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="source2.fs" /> | ||
<Compile Include="source.fs" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.