I haven't watched your videos in ages, but when I was learning c# 5 years ago I leaned so much from you and a guy called AngelSix. Always appreciated how slow you went for beginners, and taking time to teach the why as well as the how. Thanks for helping me to become a .NET Developer, good to hear your voice is still sexy
@IAmTimCorey4 ай бұрын
Thanks for sharing!
@richardjacksonxyz4 ай бұрын
GENUINELY just sat down to try incorporating some partial classes into my work and here we are! *puts kettle on*
@IAmTimCorey4 ай бұрын
Great!
@marceloleoncaceres6826Ай бұрын
Thank you Tim, your explanation about why partial classes exist is really valuable,
@IAmTimCoreyАй бұрын
You are welcome.
@daniero4 ай бұрын
As for the class’ access modifier, you can define just once and the other will use that. No need to set public (or whatever modifier used) in all definitions. That’s why the win form designer didn’t have an access modifier
@IAmTimCorey4 ай бұрын
Yep. Since they get combined together, the attributes get combined as well.
@hebreux11KC4 ай бұрын
Dear Tim you are my best one thanks you for your work and the energy you make to do this great job only God can bless you
@IAmTimCorey4 ай бұрын
Thank you!
@Rick-tg4oy4 ай бұрын
The MVVM Community Toolkit uses partial classes to combine your viewmodel class (which you have to declare as partial) to the source generator classes that it creates to hold your public properties. In the viewmodel you only declare private properties like 'name', and in your XAML you bind to 'Name', which is generated and lives in the source generator class.
@IAmTimCorey4 ай бұрын
Yep, that's a good use of partial classes.
@rikudouensof4 ай бұрын
yes. It really helps with the MVVM. it also means one will have to also use mvvm package on xaml for some special bindings
@wilbertholkema4 ай бұрын
Blazor also rely on partial classes if you use the code behind option. Using a base class, for example, on a razor file, you have to add it to both of them
@IAmTimCorey4 ай бұрын
Thanks for sharing!
@bezimienny54 ай бұрын
I feel like that was useful back in the day most for Frameworks that had a designer built in that would just generate fields, control references and whatnot, so it doesn't interfere with the code behind file that the developer is supposed to edit manually. So mostly relevant for win forms, wpf, uwp and such
@keyser4564 ай бұрын
It's useful in lots of scenarios. Adding functionality to POCOs, DTOs, scaffolded / ORM, or any auto-generated classes, but keeping it out of the boilerplate / auto-genned stuff.
@IAmTimCorey4 ай бұрын
As @keyser456 pointed out, it is really useful today when working with source generation.
@princefowzan2 ай бұрын
3:20 when creating the instance, why did you stop with new(), don't you have to add new Demo()?
@IAmTimCorey2 ай бұрын
Not in the later versions of .NET. The compiler infers the value based upon the type you specified at the beginning.
@OeHomestead4 ай бұрын
I often use partial classes when I create unit tests. Instead of having one file with a massive class in it, I split the class into several files. I also do it with mappers.
@IAmTimCorey4 ай бұрын
Interesting.
@samnihao69434 ай бұрын
We have same. Or 1 massive class or a few partial. I don't like it when is time to debug and find the error
@platoh4 ай бұрын
I'm digging these short form dives
@IAmTimCorey4 ай бұрын
Great!
@MichaelChibaka3 ай бұрын
I use partial classes for most of the code I generate from Text Templates. I have most of my API code autogenerated by text templates based on the underlying database schema. Thanks for the tutorial.
@IAmTimCorey3 ай бұрын
Yep, that's a good use for them.
@ralphtashinga4 ай бұрын
I worked on a project where the database service had many methods that were not technically related, and it became much more manageable after I organized them into separate files using partial classes.
@IAmTimCorey4 ай бұрын
If the methods aren't related, why are they in the same class? That seems more like a poor system design rather than a good use of partial classes.
@torrvic11563 ай бұрын
Thank you so much for your insight, Mr. Corey! You reminded me of how WinForms works in this video.
@IAmTimCorey3 ай бұрын
You are welcome.
@ChrisWard744 ай бұрын
Can the Solution Explorer show your partial classes in a hierarchy like it does for the Form classes? So like have a main Demo partial class and then under it all of your other partial classes to organize it more?
@holger_p4 ай бұрын
It's just about organizing files on disk. Git is also file based. If you have a long class definition, you can put the "forever"-methods into one file, and the more likely to change in another. You would keep track, a part of the class is unchanged for a long time, what git usually doesn't provide.
@IAmTimCorey4 ай бұрын
No, it isn't about organizing files on disk. It is mostly about separating out generated code that gets regenerated from hand-crafted code which should persist, as I pointed out in this video.
@holger_p4 ай бұрын
@@IAmTimCorey sure, that's the known application. But if you say that's the only use, you should explictly say " never use it manually in your programming, you just explain how to read it. " So your answer when to use it, is never ( manually ).
@IAmTimCorey4 ай бұрын
No, I'm not saying to never use it manually. There are times when it is valuable to do so. The example you listed, however, is not one that I would recommend.
@benjaminshinar95094 ай бұрын
I'm recently getting suggestions by the VS editor to move my regex into some compiled form, and it's making my class into partials. could you explain the mechanism they use?
@TheJessejunior4 ай бұрын
very good, I've been looking for this answer for long :)
@IAmTimCorey4 ай бұрын
I am glad it was helpful.
@JimmyGee4 ай бұрын
Thanks! I like those short explainers.
@jonathancumini61834 ай бұрын
Could be way shorter in my opinion
@IAmTimCorey4 ай бұрын
I'm glad you enjoy them.
@clebersondot-net4 ай бұрын
thanks tim!
@IAmTimCorey4 ай бұрын
You are welcome.
@OniFeez4 ай бұрын
Can you use partial classes to prevent circular redundancy for solutions with multiple projects? Say you have a class library project and you have some sort of executable project, and you have some sort of file loading loading system in the executable project but you need to use some of the schemas you designed in the class library, could that be used as a sort of 'partial'? Similar in concept I guess to forward declarations in c++?
@IAmTimCorey4 ай бұрын
No, that would not work. Remember, in the video I said that a partial class is for design-time. At build, the two partial classes get combined into one actual class.
@pedrobettt4 ай бұрын
I'd love to see at least the official documentation, and maybe even additional info from other sources, in the description. It would help to allow people to dive further into the topic.
@IAmTimCorey4 ай бұрын
A quick Google search will get you the documentation. Or go to docs.com and search there (that's the official Microsoft documentation and training site). The thing is, there is often more than one article on Microsoft's site about a particular topic. There isn't always one official document.
@jasoncox72444 ай бұрын
When I use `partial class` it is usually because I have a class that is implementing multiple interfaces (that have more than one or two methods to implement), and I will separate out those interfaces into separate files, just for my personal ease of reading. This is one of my favorite syntactic-sugar features of C# lol. For example, any time a class implements IEnumerable non-trivially, that goes in a separate file (and the Extension methods go in that file too).
@IAmTimCorey4 ай бұрын
That's not why it is there, but I'm glad it works for you.
@jasoncox72444 ай бұрын
@@IAmTimCorey I know~ but I appreciate being able to `abuse` ... ahem... find the save use edges of the grey areas... of features ;) lol
@refactorear4 ай бұрын
Back when we had to interface with COM components from NET Framework we would create the coclass as a partial class with the COM forwarding and the different properties so that the logic would stay as clean as possible. Once we stopped maintaining that interface we just deleted the CoClass directory with all the partial classes. Now we are starting to use it with code generators indeed.
@IAmTimCorey4 ай бұрын
Thanks for sharing!
@DadDoesCode4 ай бұрын
Interesting. For game development, would this be good for something like a Player class and separate partial classes of Player that handle individually Movement, Input, Animation, etc?
@holger_p4 ай бұрын
There are no "partial classes". It's one class, just split in multiple files.
@DadDoesCode4 ай бұрын
@@holger_p It would all be "Player", that is clearly understood when I stated the other files would be stemming from Player as a partial. My question is not what a partial class is, it is specifically about if using partial would make sense for the use case I stated. Or, would a more SOLID approach make more sense to specifically designate separate unique classes for those modules, which is what i'd normally do.
@holger_p4 ай бұрын
@@DadDoesCode if having 2 files with 1000 lines of source code is different to having 1 file with 2000 lines for you, then yes. If you construct classes with 5000lines of code maybe. But it's not common to have such huge classes or maximize the number of files.
@IAmTimCorey4 ай бұрын
This isn't about file organization. It is mostly about separating generated code that will get regenerated from user-generated code which should be persisted. Using partial classes as a file organization plan is a bad idea. It just hides the true scope of your class and makes it harder to track down issues.
@rik22433 ай бұрын
Personally I find Partial classes very useful, as I auto generate a lot my code, such as models from the Database, but sometimes I need to add extra properties that would get overwritten when I regenerate the model. By adding a partial extra class I can retain these extra properties.
@IAmTimCorey3 ай бұрын
Yep, that’s great!
@ThunderChasers4 ай бұрын
I have encountered instances when I HAD to modify the designer file. It isn't a big deal as long as you understand what everything is.
@IAmTimCorey4 ай бұрын
Yep, I've showed how to do that a number of times. You need to be careful, because you can cause issues, but there is a reason why we have access to the file.
@ThunderChasers4 ай бұрын
@@IAmTimCorey you're the man! Thanks for the reply.
@thespecialistt2154 ай бұрын
I was asked in a technical interview the reason for partial classes 12 years ago. I didn't know the answer but was told it was so you can have more than one developer working on the same class as it merges them. The person asking it told me he didn't get the answer right when he was asked. It was a large electronics distributor. Is this not the reason originally?
@IAmTimCorey4 ай бұрын
No, that was never the reason. Yes, technically if you were using TFS or another version control system where file locking occurs, you could use partial classes to get around the file locking, but that's an abuse of the system and not what it was designed for. Your version control system should not dictate the way you write code like that, either. If you had this issue, you could use git instead and the problem goes away. Everyone can work on the same file. No locking occurs.
@antoniusivan87674 ай бұрын
Hey tim, I already successfully create Indonesia user group. It already has 108 members from beginner to pro. From 18 y.o until 70 y.o.and already has meetup.
@IAmTimCorey4 ай бұрын
Great!
@kevinnowicki38304 ай бұрын
I use partial classes all the time. The main reason to use it is when more then one developer work on the same project. If one of us is working on module A and other on module B and we have one class for DB access than we can split code into different files, e.g. DBAccess.A.cs and DBAccess.B.cs. We will get both files grouped together in the solution explorer view and there will be no conflicts when we will merge together code from both developers.
@IAmTimCorey4 ай бұрын
That's an abuse of the system and not really necessary if you are using a distributed version control like git. If two developers are truly working on two different parts of a class, they won't have merge conflicts anyway. Besides, if your classes are that big, you probably need to consider refactoring.
@nonameno20064 ай бұрын
Just use git
@ChillGuyz4 ай бұрын
❤
@IAmTimCorey4 ай бұрын
Thank you!
@Arvydas19874 ай бұрын
explainned
@IAmTimCorey4 ай бұрын
👍
@SteffenSkov4 ай бұрын
I would be wary about using partial classes for anything else than allowing expanding generated code. If your class' source file is getting too big, spilt up the class, not just the source file! Spreading your class source code across several files is not helpful in understanding what your class does.
@IAmTimCorey4 ай бұрын
Yeah, this definitely isn't for splitting up your code because your code is too big. That's not why it exists.
@jayrigger75084 ай бұрын
I use a name like Demo.Other.cs based off the class it is partial of
@IAmTimCorey4 ай бұрын
That can work.
@sanjeevsjk4 ай бұрын
1st
@IAmTimCorey4 ай бұрын
👍
@CSharpProdigy4 ай бұрын
3rd
@IAmTimCorey4 ай бұрын
👍
@jouchiwinchester26664 ай бұрын
2nd
@IAmTimCorey4 ай бұрын
👍
@droberts79422 ай бұрын
What are your thoughts on using dot notation for these classes? For example, Service.CS being the main class, and then naming them Service.Authorization.cs Service.Requests.cs and Service.DoSomethingElse.cs?
@IAmTimCorey2 ай бұрын
I've tried to like the dot notation for a while and I just don't find it that great, typically. If I really wanted those items to be grouped together, I would put them in the Service folder and then they would conform to your naming convention without needing the class name itself to contain the dot.
@Nei月牙天衝-y7u4 ай бұрын
There is no use for partial class other than making the system overly complex to understand. You're hurting SOLID principles just by having it. If your class is too big to contain it's own logic, you're doing something wrong.
@IAmTimCorey4 ай бұрын
Did you actually watch the video or are you just making wild guesses? There is absolutely uses for partial classes. They are most often around source-generated classes that can also be modified by developer. I covered that in this video. One type of example of this is the code behind for a Windows Form that also interacts with the designer-generated code for the form layout. They use partial classes. Also, most source generators use partial classes because every time you generate the source, you destroy what was there previously, yet developers need to be able to customize that generated code. That's why they use partial classes. The developer-modified partial can stay even while the other partial gets overwritten with the new generation. As for SOLID principles, they have nothing to do with partial classes. You do realize, as I mentioned in the video, that partial classes are just for design purposes and that they get combined when the code is compiled, right? This has nothing to do with being too big. It never has been.
@Nei月牙天衝-y7u4 ай бұрын
@@IAmTimCorey I did. I don't know why you always come in the comments section sounding aggressive, but for a regular API application, there is no use. OK, WinForms can leverage it. OK, the way C# works under the hood uses it. Regardless, using it in a daily basis for most of projects ends up with what I have said. SOLID principles have everything to do with partial classes, why are you naming parts of your class that does different things here and there? I love technical discussions, I love the fact that you actually answer comments, but I am frightened that you came to confront instead of discuss
@IAmTimCorey4 ай бұрын
I tend to respond to comments with the energy they bring. What you were saying was definitive, but also incorrect. Making definitive statements is rather abrupt, especially when you say something like "there is no use for..." Again, you came back with "for a regular API application, there is no use." That too is incorrect. Code generation is the primary use for partial classes. Entity Framework uses code generation. So do lots of other libraries. That means there absolutely is a value in having partial classes. Here's a piece of advice - when a company implements a language keyword like partial, assume it has a purpose. Quite a few smart people agonized over including it or not and how to implement it. They had lots of data to back up their decision. You should have a REALLY good case to say that a feature is useless. Yes, teams of people make mistakes and teams make poor choices. But that's actually the vast minority of the time. You can not agree with their reasons, but they often have well thought out reasons.
@Nei月牙天衝-y7u4 ай бұрын
I'd argue a lot with this but I understand what you're saying. What I wanted to say is something that you said in the video yourself: most of the cases you're not gonna need it. Thanks for ideas shared. Keep it the good work.