Skip to content

Commit 9976a75

Browse files
authored
Merge pull request #1 from ibrahimatay/CreateUnboundedPrioritized
Channel.CreateUnboundedPrioritized Method
2 parents 2a5e7c5 + feaaa29 commit 9976a75

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed
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>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

CreateUnboundedPrioritized/Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Channel.CreateUnboundedPrioritized Method
2+
// https://learn.microsoft.com/en-us/dotnet/api/system.threading.channels.channel.createunboundedprioritized?view=net-9.0
3+
4+
// [API Proposal] Channel.CreateUnboundedPrioritized #62761
5+
// https://github.com/dotnet/runtime/issues/62761
6+
7+
// Concurrent collection with priority
8+
// https://stackoverflow.com/questions/23470196/concurrent-collection-with-priority
9+
10+
using System.Threading.Channels;
11+
12+
var channel = Channel.CreateUnboundedPrioritized<(int priority, string message)>();
13+
14+
// Write sample messages
15+
await channel.Writer.WriteAsync((2, "Emergency service dispatch"));
16+
await channel.Writer.WriteAsync((1, "Routine check: Blood pressure measurement"));
17+
await channel.Writer.WriteAsync((3, "Heart attack alert!"));
18+
await channel.Writer.WriteAsync((3, "Fire alarm!"));
19+
20+
channel.Writer.Complete();
21+
22+
// Read and process messages based on priority
23+
await foreach (var item in channel.Reader.ReadAllAsync())
24+
{
25+
Console.WriteLine($"Processing: {item.message} (Priority: {item.priority})");
26+
}

CsharpLangExamples.sln

+12
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoslynAnalyzer.Tests", "Ros
9393
EndProject
9494
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Roslyn Analyzer", "Roslyn Analyzer", "{A20B336B-C5A8-46EC-A4D3-843C257A4809}"
9595
EndProject
96+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NewLINQMethodsCsharp10", "NewLINQMethodsCsharp10\NewLINQMethodsCsharp10.csproj", "{D7C23C3C-8B11-4E31-830F-17CD9670D49E}"
97+
EndProject
98+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CreateUnboundedPrioritized", "CreateUnboundedPrioritized\CreateUnboundedPrioritized.csproj", "{8D54CE39-9351-4F21-B5A2-1238973E63B3}"
99+
EndProject
96100
Global
97101
GlobalSection(SolutionConfigurationPlatforms) = preSolution
98102
Debug|Any CPU = Debug|Any CPU
@@ -271,6 +275,14 @@ Global
271275
{9C9BBC19-CDBA-4851-B3D8-8C383F4742FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
272276
{9C9BBC19-CDBA-4851-B3D8-8C383F4742FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
273277
{9C9BBC19-CDBA-4851-B3D8-8C383F4742FE}.Release|Any CPU.Build.0 = Release|Any CPU
278+
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
279+
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Debug|Any CPU.Build.0 = Debug|Any CPU
280+
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Release|Any CPU.ActiveCfg = Release|Any CPU
281+
{D7C23C3C-8B11-4E31-830F-17CD9670D49E}.Release|Any CPU.Build.0 = Release|Any CPU
282+
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
283+
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
284+
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
285+
{8D54CE39-9351-4F21-B5A2-1238973E63B3}.Release|Any CPU.Build.0 = Release|Any CPU
274286
EndGlobalSection
275287
GlobalSection(SolutionProperties) = preSolution
276288
HideSolutionNode = FALSE
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>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

NewLINQMethodsCsharp10/Program.cs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+

2+
// Chunk.cs
3+
// https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Chunk.cs
4+
5+
// how do I chunk an enumerable?
6+
// https://stackoverflow.com/questions/12389203/how-do-i-chunk-an-enumerable
7+
8+
var movies = new List<Movie>
9+
{
10+
new Movie("Titanic", 1998, 4.5f),
11+
new Movie("The Fifth Element", 1997, 4.6f),
12+
new Movie("Terminator 2", 1991, 4.7f),
13+
new Movie("Avatar", 2009, 5),
14+
new Movie("Platoon", 1986, 4),
15+
new Movie("My Neighbor Totoro", 1988, 5)
16+
};
17+
18+
Console.WriteLine("---------Chunk-----------");
19+
20+
// Chunk
21+
foreach (var movie in movies.Where(movie => movie.Rating > 4.5f).Chunk(3))
22+
{
23+
Console.WriteLine(string.Join(",", movies.Select(currentMovie => currentMovie.Name)));
24+
}
25+
26+
Console.WriteLine("---------DistinctBy-----------");
27+
28+
// DistinctBy
29+
foreach (var movie in movies.DistinctBy(movie => movie.Rating))
30+
{
31+
Console.WriteLine(string.Join(",", movies.Select(currentMovie => currentMovie.Name)));
32+
}
33+
34+
Console.WriteLine("---------Take-----------");
35+
36+
// Take with Ranges
37+
foreach (var movie in movies.Take(^5..3))
38+
{
39+
Console.WriteLine(string.Join(",", movies.Select(currentMovie => currentMovie.Name)));
40+
}
41+
42+
record Movie(string Name, int Year, float Rating);

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Examples of C# programming, to track and stay up-to-date with the latest languag
2020
* `Index()`
2121
* `CountBy()`
2222
* `AggregateBy()`
23-
23+
* [CreateUnboundedPrioritized](CreateUnboundedPrioritized/Program.cs)
24+
2425
* C# 12 is supported on .NET 8 (November, 2023)
2526
* [Primary constructors](PrimaryConstructors/Program.cs)
2627
* [Default values for parameters in lambda expressions](DefaultLambdaParameters/Program.cs)
@@ -35,6 +36,11 @@ Examples of C# programming, to track and stay up-to-date with the latest languag
3536

3637
* C# 10 is supported on .NET 6 (November, 2021)
3738
* [Global using directive](GlobalUsingDirective/Program.cs)
39+
* API Improvements
40+
* [New LINQ Methods](NewLINQMethodsCsharp10/Program.cs)
41+
* `Chunk()`
42+
* `DistinctBy()`
43+
* `Take()`
3844

3945
* C# 9 is supported on .NET 5 (November, 2020)
4046
* [Target-typed new expressions ](TargetTypedNewExpressions/Program.cs)

0 commit comments

Comments
 (0)