Posts

Showing posts from January, 2025

Asynchronous Programming

Image
Creating the Asynchronous App Via terminal: dotnet new console --framework net9 .0 -n SyncBreakfast Then, open the project with your favorite IDE, and creates a new folder called “Models” and inside it creates the class below: namespace SyncBreakfast.Models ; public class Breakfast { public Coffee Coffee { get ; set ; } = new Coffee(); public Bacon Bacon { get ; set ; } = new Bacon(); public Egg Egg { get ; set ; } = new Egg(); public Juice Juice { get ; set ; } = new Juice(); } public class Coffee { public Coffee PourCoffee ( int cup ) { Console.WriteLine( $"Pouring {cup} of coffee" ); Task.Delay( 1000 ).Wait(); return new Coffee(); } } public class Bacon { public Bacon FryBacon ( int slices ) { for ( int slice = 0 ; slice < slices; slice++) { Console.WriteLine( "Cooking a slice of bacon" ); Task.Delay( 2000 ).Wait(); } r...

ICollection vs IQueryable vs IEnumerable vs IList vs List vs HashSet in C#

Image
                                    When working with data in C#, choosing the proper collection type is critical for achieving best performance, maintainability, and code clarity. This article discusses collection interfaces and types in C#, including differences, use cases, and recommended practices. IEnumerable Definition : Represents a sequence of elements that can be enumerated. Key Features : Supports simple iteration over a collection using foreach. Provides deferred execution when used with LINQ. Read-only; does not support adding or removing elements. When to Use : Ideal for exposing data that does not need to be modified. Suitable for querying collections using LINQ. IEnumerable< int > numbers = new List< int > { 1 , 2 , 3 , 4 , 5 }; foreach ( var number in numbers) { Console.WriteLine(number); } IReadOnlyCollection Definition : Represents a read-only collection of el...