Тёмный

10 Essential Constructors in C# Every Developer Should Know 

Zoran Horvat
Подписаться 30 тыс.
Просмотров 5 тыс.
50% 1

Creating objects in C# is more than just instantiating classes-it's an art that involves understanding the myriad ways constructors can shape and initialize your objects. In this journey, we delve deep into the world of constructors, revealing techniques and insights that can transform the way you see object construction. There are many details to make right in every class you design, and this video is the go-to place to learn the most important ones.
Become a sponsor to access source code ► / zoranhorvat
Enroll course Beginning Object-Oriented Programming with C# ► codinghelmet.c...
Join Discord server with topics on C# ► codinghelmet.c...
Subscribe ► / @zoran-horvat
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️COPYRIGHT NOTICE:
The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorized to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.

Опубликовано:

 

11 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 40   
@leninparedes6207
@leninparedes6207 День назад
Zoran, you're my copilot, seriously! Thank you so much.
@kristianaranda
@kristianaranda День назад
Well, seriously, it's a great idea. A gpt model trained with Zoran's knowledge! 😜
@KaineVarley
@KaineVarley День назад
I always learn something from your fabulous videos Zoran, even as a senior dev with decades of experience... Thank you
@CRBarchager
@CRBarchager День назад
For once I got to 90% of the video feeling "I got this" and then fall short as I've never used copy constructors before. Not knowingly atleast. You learn something new every time you watch a video from Zoran. Thank you for sharing as always.
@ChrisWalshZX
@ChrisWalshZX День назад
What a brilliant round up! I suspected that copy contructors might be able to access private fields/methods of the other object but not got around to confirming this. Thanks Zoran.
@zoran-horvat
@zoran-horvat День назад
@@ChrisWalshZX That is the only way to get to them - through a member of that class. And what better member there is than a constructor itself.
@haraheiquedossantos4283
@haraheiquedossantos4283 День назад
I hope to watch part 2 (all the other possibilities) 👀
@zoran-horvat
@zoran-horvat День назад
@@haraheiquedossantos4283 I hope to record them!
@serviceengine
@serviceengine День назад
Another intriguing thing for me about constructors was a moment when I realized that they could not be asynchronous. Even an async factory method eventually creates an object synchronnously. The concept of asynchronous constructor and object that is being under construction for some time that is growing and growing to finally get "born" sounds cool, but I couldn't find a good use case for that.
@zoran-horvat
@zoran-horvat День назад
@@serviceengine That is a topic for another video, which I already have under construction, which covers what should and should not be the part of a constructor.
@serviceengine
@serviceengine День назад
@@zoran-horvat Looking forward then. I will stop spoiling your future plans again😉
@zoran-horvat
@zoran-horvat День назад
@@serviceengine No, don't stop. I had no plans to mention async methods, but now that you have put them into the context of constructors I have something interesting to think about.
@serviceengine
@serviceengine День назад
@zoran-horvat asynchronous constructors concept kinda overlapping with my previous comment about the amount of logic inside constructors now. Imagine some async method executed in the constructor in the sync way like GetAwaiter()... Now, if the construction of an object is taking a minute, for example, it will be so, so destructive.
@serviceengine
@serviceengine День назад
I'm wondering if there is some kind of "runtime limbo" when object construction is so long. Exactly between moments when the creation starts and when memory is allocated at the end of the constructor execution.
@tarsala1995
@tarsala1995 День назад
As usual, great and instructive video. Sometimes I forget the power of C#, this is a good reminder :)
@marklord7614
@marklord7614 День назад
I still learned something new even about a topic I thought I had a good handle on.
@TheBabbl978
@TheBabbl978 День назад
You didn't mention about the most tricky constructor: static constructor
@zoran-horvat
@zoran-horvat День назад
@@TheBabbl978 Possibly on a separate video. Static constructors fall into a niche that has its own rules.
@Thorarin
@Thorarin День назад
Technically not a constructor, though. It's called a type initializer.
@nGAGE0nline
@nGAGE0nline День назад
I wonder if you consider the De~constructor as another type of constructor. Regardless, I'd love to hear your thoughts on when to use a deconstructor vs IDisposable, and if there's even still a good use-case for deconstructors these days.
@zoran-horvat
@zoran-horvat День назад
@@nGAGE0nline I don't remember using deconstructors ever in my code.
@malzsmith
@malzsmith День назад
Love your videos, Zoran! Do you have any experience working with F# I've seen you reference it a few times in your other videos. Could you recommend some good resources for learning best practices?
@zoran-horvat
@zoran-horvat 22 часа назад
You can follow Scott Wlaschin for F#. His site fsharpforfunandprofit.com is indispensable.
@chrisspire
@chrisspire День назад
Pure gold.
@serviceengine
@serviceengine День назад
Really cool video! I would like to hear more about the accepted amount of logic in constructors themselves. I mean, we all know that except validation or simple assignment logic, it is better to avoid more complex logic in constructors . Is there some rule of thumb for that? I faced many times that the constructor executed a setting method, and that method was quite complex. It was setting many properties from different fields and other helper methods, and those from other and so on. Where could an acceptable limit for a constructors logic to handle?
@zoran-horvat
@zoran-horvat День назад
@@serviceengine That is a very good question. Actually, I do have a script for that video sitting in the queue for quite some time. The short answer is that I prefer static factory methods when there is a substantial chunk of work to do other than, say, just collecting some input items into a private collection, etc. The very fact that the caller must call a method already rings the bell that there might be a cost that the caller should not ignore. For example, creating many objects in a loop is assumed to be cheap. But calling a factory method many times in a loop could be a different story, and we should consult with that method's documentation before deciding.
@dono42
@dono42 День назад
The DisplayName = $"{firstName} {lastName}" code is functionally problematic: 1) it assumes the first name (=given name) is displayed prior to the last name (=surname) and 2) it assumes a space is used between first and last names. Neither are correct in, for example, Japanese; it should be last name followed by first name and usually there is no space. (And if space is used it is the not same space being used here.)
@zoran-horvat
@zoran-horvat День назад
@@dono42 You are both right and wrong. The wrong part is in assuming those cases apply - it is the customer's requirements that define the domain of application, and, consequently, the cases that apply. The cases you mentioned are valid, but if none applies to the software you are developing, then you won't implement them!
@DavidSmith-ef4eh
@DavidSmith-ef4eh День назад
Cool, but now Buddha can't register into your app, because birthyear can't be negative, and he was born 500 bc....
@zoran-horvat
@zoran-horvat День назад
@@DavidSmith-ef4eh True. It is important to recognize the limitations and improve the model on the way.
@DavidSmith-ef4eh
@DavidSmith-ef4eh День назад
@@zoran-horvat sorry, you didn't say anything controversial in this one, so I had to make something up. :D
@zoran-horvat
@zoran-horvat День назад
@@DavidSmith-ef4eh Of course.
@7th_CAV_Trooper
@7th_CAV_Trooper День назад
Buddha has transcended the need for software.
@ghevisartor6005
@ghevisartor6005 День назад
@@zoran-horvat would you have made the birthYear into a domain object with Year and CalendarEra? (i'm not sure is the proper name)
@promant6458
@promant6458 2 дня назад
I always wondered, why include a private setter in your properties, even though they are set only from a constructor? It's like using a field and not marking it as readonly... Is there something I am missing?
@raduban2029
@raduban2029 День назад
The owning object can still change the value of the property in its methods besides the constructor
@zoran-horvat
@zoran-horvat День назад
@@promant6458 We do that in mutable classes. I wasn't developing the class's behavior further in this demo, but that is where we would supposedly change the values of properties. On the other hand, there is something in your comment. My usual process is to leave properties readonly until it is proven they need a setter.
@weluvmusicz
@weluvmusicz 2 дня назад
First!
Далее
What Good Is an Empty Interface?
7:01
Просмотров 15 тыс.
Hurricane Milton: Storm damage in Fort Myers, Fla.
01:05
The New Python 3.13 Is FINALLY Here!
20:39
Просмотров 17 тыс.
Coding Was HARD Until I Learned These 5 Things...
8:34
Nothing is really cool in Kotlin
7:43
Просмотров 7 тыс.
Getting Started with Dapper in .NET
23:42
Просмотров 16 тыс.
What does '__init__.py' do in Python?
6:50
Просмотров 14 тыс.
Microservices are Technical Debt
31:59
Просмотров 487 тыс.