Extension Methods for Structs

  Рет қаралды 1,184

Coding Tutorials

Coding Tutorials

Күн бұрын

Value types pass by value and reference types pass by reference. But interfaces are implemented by both value and reference types. How are they passed?
Source code available at: github.com/Jas...
Topics include:
C# extension methods
Value types
Immutable types
Readonly structs
Passing by ref
Generic constraints
Boxing
Interfaces on structs

Пікірлер: 19
@petrusion2827
@petrusion2827 2 жыл бұрын
Here's how to do it without code duplication: private static void PrivateEnnoble(ref T nameable) where T : INameable { nameable.Name = "Sir " + nameable.Name; } public static void Ennoble(this INameable nameable) { PrivateEnnoble(ref nameable); } public static void Ennoble(this ref T nameable) where T : struct, INameable { PrivateEnnoble(ref nameable); }
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
That's very neat - thanks!
@davidwhite2011
@davidwhite2011 2 жыл бұрын
Another beautiful teaching example.
@piurek10
@piurek10 2 жыл бұрын
You provide a lot of good stuff. I do not understand why you have so few viewers.
@youtischia
@youtischia 2 жыл бұрын
Great video as all your videos are. I reckon quite a few will avoid this feature as it is too easy to get wrong. All that work just for an extension method on a struct that does not blow up.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
'readonly struct' is the simple answer
@aykcavusyan2001
@aykcavusyan2001 2 жыл бұрын
Beautiful 🤩, as always , thanks
@danixadem
@danixadem 2 жыл бұрын
Thanks a lot, that was really helpful.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
Structs have quite a few gotchas. Any others you've spotted? Source code at: github.com/JasperKent/Extension-Boxing Remember to subscribe at kzbin.info/door/qWQzlUDdllnLmtgfSgYTCA And if you liked the video, click the 👍.
@--Eric--
@--Eric-- 2 ай бұрын
Why don't you always pin this information to all of your excellent videos 🤔
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 ай бұрын
@@--Eric-- Which information?
@finwwwfinwww4669
@finwwwfinwww4669 2 жыл бұрын
Thanks :)
@rodrigo6459
@rodrigo6459 2 жыл бұрын
I know this is not related to this video… but Can you show how to work with more than 1 DbContext for code first using Repository?
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
Depends what you're trying to achieve with multiple DbContexts.
@rodrigo6459
@rodrigo6459 2 жыл бұрын
@@CodingTutorialsAreGo one is for the database that holds the WebAPI app information, the other will be used for JWT authentication and authorization. The are both totally independent databases.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
@@rodrigo6459 Got you. I'll put it on the to do list.
@rodrigo6459
@rodrigo6459 2 жыл бұрын
@@CodingTutorialsAreGo so glad you are doing this in some near future! 🥹
@djyotta
@djyotta 2 жыл бұрын
Why not (as a better rule of thumb) just have extension methods always be of the form: public static IMyInterface MyExtensionMethod(this IMyInterface in) That way, it will work the "same" for struct/class, and also work for chaining methods.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 2 жыл бұрын
So you're saying, just make everything immutable? That's going to have problems with transferring references from the original object to the new one.
Blazor RenderFragment
23:11
Coding Tutorials
Рет қаралды 3 М.
C# 12 Collection Expressions and the Spread Operator
25:27
Coding Tutorials
Рет қаралды 2,3 М.
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 920 М.
you will never ask about pointers again after watching this video
8:03
.NET Worker Services and Dependency Injection
18:45
Coding Tutorials
Рет қаралды 1,8 М.
Every single feature of C# in 10 minutes
9:50
Train To Code
Рет қаралды 177 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 347 М.
C# 12 Spread Operator Performance Pitfalls
12:53
Coding Tutorials
Рет қаралды 1,3 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 201 М.
REFERENCES in C++
10:13
The Cherno
Рет қаралды 599 М.
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН