10 Essential Constructors in C# Every Developer Should Know

  Рет қаралды 5,357

Zoran Horvat

Zoran Horvat

Күн бұрын

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.

Пікірлер: 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 23 сағат бұрын
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 21 сағат бұрын
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 23 сағат бұрын
@@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 Күн бұрын
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 Күн бұрын
First!
Will You Lose the Advantages of OOP to an Anemic Domain Model?
13:08
The New Python 3.13 Is FINALLY Here!
20:39
ArjanCodes
Рет қаралды 17 М.
小蚂蚁会选到什么呢!#火影忍者 #佐助 #家庭
00:47
火影忍者一家
Рет қаралды 58 МЛН
Running With Bigger And Bigger Lunchlys
00:18
MrBeast
Рет қаралды 133 МЛН
Life hack 😂 Watermelon magic box! #shorts by Leisi Crazy
00:17
Leisi Crazy
Рет қаралды 78 МЛН
Boolean Is Not Your Friend
12:45
Zoran Horvat
Рет қаралды 42 М.
God-Tier Developer Roadmap
16:42
Fireship
Рет қаралды 7 МЛН
Main Hall 06: Why is Functional Programming so hard?
37:44
DDD Melbourne
Рет қаралды 4,1 М.
Here Is the Most Powerful EF Core Configuration Technique
16:05
Zoran Horvat
Рет қаралды 11 М.
Math News: The Bunkbed conjecture was just debunked!!!!!!!
14:59
Dr. Trefor Bazett
Рет қаралды 140 М.
Why Isn't Functional Programming the Norm? - Richard Feldman
46:09
What Good Is an Empty Interface?
7:01
Zoran Horvat
Рет қаралды 15 М.
Plastic Makers Have A Big Secret: They’re Experimenting On You
15:35
More Perfect Union
Рет қаралды 167 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 487 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 314 М.