.NET 8 New Features with
Code Examples
Mert Metin
D E V N O T D O T N E T K O N F E R A N S I 2 0 2 4
Slide 2
Slide 2 text
Mert Metin
- Senior Software Engineer
A B O U T M E
mrtmtn
_mertmetin
mertmtn
Blogcu Mühendis
- Blogger
QR to reach me
Slide 3
Slide 3 text
01 02 03
Performance
Improvements
Keyed DI Services
C# 12
- Primary Constructors
- Collection Expressions
- Spread Operator
- Data Annotations
- Default Values in Lambda Exp.
- Exprerimental
- Random.GetItems
- Shuffle
- Alias Any Type
Enhanced Feature for
.NET Dependency
Injection
Agenda
- FrozenSet
- CompositeFormat
- SearchValues
- Benchmark Results*
of the Improvements
*All results have created by BenchmarkDotnet tool
Slide 4
Slide 4 text
Primary Constructors
- Simple class creation
- Looks like method parameter
Primary Constructor
Typical constructor before C# 12
Slide 5
Slide 5 text
- Useful for dependency injection
based projects
- Reduces code lines
- Increases readability
Slide 6
Slide 6 text
Collection Expressions
Initialization and defining operations are more concise and clear.
Slide 7
Slide 7 text
Spread Operator
Merge, clone, extend collections with two dots (..)
Reminds us of Javascript
Slide 8
Slide 8 text
Data
Annotations
Added new attributes
Length
Range
AllowedValues
DeniedValues
Base64String
Slide 9
Slide 9 text
Default Values in Lambda Exp.
Before C# 12
default parameter
Current
Output
Slide 10
Slide 10 text
Experimental Attribute
An attribute which specifies as experimental feature on method or class
Belongs to System.Diagnostics.CodeAnalysis
Slide 11
Slide 11 text
Experimental Attribute
Suppress the warnings
Source Code
Using Pragma Blocks
Slide 12
Slide 12 text
Random.GetItems
Selects a specific number of items from a given set of elements randomly
Drawback is;
Resulting random collection can include duplicate entries.
.
Output
Unexpected
Output
Slide 13
Slide 13 text
Random.GetItems
Flagging mechanisim using dictionary to avoid duplications
Slide 14
Slide 14 text
Shuffle
Performs an in-place shuffle of an array.
Useful in some cases;
machine learning,
gambling,
online exam
Belongs to Random class
Slide 15
Slide 15 text
Shuffle - Code Examples
Slide 16
Slide 16 text
Alias Any Type
Creating custom type with “using”
keyword.
“global using” are required for using
everywhere
Slide 17
Slide 17 text
Keyed DI Services
- Naming services with key.
- Same service - different named services or different lifetimes
Key, could be string or enum type
Program.cs service declaration
Slide 18
Slide 18 text
Keyed DI Services - Usages
FromKeyedServices - Given key implemented on ActionMethod
Program.cs service declaration
Slide 19
Slide 19 text
Factory Method
Implementation
Slide 20
Slide 20 text
FrozenSet
- Frozen means immutable
- Impossible to manipulation - add,clear,remove operations
Slide 21
Slide 21 text
FrozenSet
Benchmark
Results
- Slower creation
because of modification the set
- Faster lookup or reading
because of immutable set
Slide 22
Slide 22 text
CompositeFormat
Allows to parse a composite format string once and reuse this instance multiple times.
This approach significantly enhances performance, when the format string has created
dynamically but used repeatedly.
Slide 23
Slide 23 text
SearchValues
Optimized in scenarios where the
same set of values is frequently used
for searching at runtime.
Currently, only byte and char
supported
“String list” will be overloaded
in .NET 9
Slide 24
Slide 24 text
int.ToString()
Benchmark Results of the Improvements
Slide 25
Slide 25 text
Benchmark Results of the Improvements
string.Replace()