Skip to content

Commit

Permalink
Merge pull request #10061 from dotnet/main
Browse files Browse the repository at this point in the history
Merge main into live
  • Loading branch information
dotnet-policy-service[bot] committed Jun 25, 2024
2 parents c62189e + d6d38e4 commit b5d74a7
Show file tree
Hide file tree
Showing 190 changed files with 6,662 additions and 11 deletions.
6 changes: 3 additions & 3 deletions includes/csharp-interactive-with-utc-note.md
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.
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>
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>
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>
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>
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>
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>
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>
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>
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>
Loading

0 comments on commit b5d74a7

Please sign in to comment.