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
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
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.
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
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.
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.
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
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.
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.
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?
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?
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.
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.
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.
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.
@@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 ).
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.
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.
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++?
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.
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).
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.
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.
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?
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.
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_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.
@@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.
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.
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.
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.
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.
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.
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.
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.
@@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
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.
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.
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?
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.
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
Thanks for sharing!
GENUINELY just sat down to try incorporating some partial classes into my work and here we are!
*puts kettle on*
Great!
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
Yep. Since they get combined together, the attributes get combined as well.
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
Thank you!
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.
Yep, that's a good use for them.
Thank you so much for your insight, Mr. Corey! You reminded me of how WinForms works in this video.
You are welcome.
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
Thanks for sharing!
I'm digging these short form dives
Great!
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.
Yep, that's a good use of partial classes.
yes. It really helps with the MVVM. it also means one will have to also use mvvm package on xaml for some special bindings
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.
Interesting.
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
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
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.
As @keyser456 pointed out, it is really useful today when working with source generation.
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.
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.
Thanks! I like those short explainers.
Could be way shorter in my opinion
I'm glad you enjoy them.
very good, I've been looking for this answer for long :)
I am glad it was helpful.
thanks tim!
You are welcome.
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?
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?
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.
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.
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.
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.
@@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 ).
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.
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.
Thanks for sharing!
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++?
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.
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).
That's not why it is there, but I'm glad it works for you.
@@IAmTimCorey I know~ but I appreciate being able to `abuse` ... ahem... find the save use edges of the grey areas... of features ;) lol
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.
Yep, that’s great!
3:20 when creating the instance, why did you stop with new(), don't you have to add new Demo()?
Not in the later versions of .NET. The compiler infers the value based upon the type you specified at the beginning.
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.
Great!
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?
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.
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?
There are no "partial classes". It's one class, just split in multiple files.
@@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.
@@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.
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.
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.
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.
@@IAmTimCorey you're the man! Thanks for the reply.
❤
Thank you!
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.
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.
Just use git
explainned
👍
I use a name like Demo.Other.cs based off the class it is partial of
That can work.
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.
Yeah, this definitely isn't for splitting up your code because your code is too big. That's not why it exists.
1st
👍
3rd
👍
2nd
👍
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.
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.
@@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
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.
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.
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?
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.