Partial Classes in C# - What they are for, how to use them, and more

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 พ.ย. 2024

ความคิดเห็น • 89

  • @acidhauss7018
    @acidhauss7018 2 หลายเดือนก่อน +4

    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

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +2

      Thanks for sharing!

  • @richardjacksonxyz
    @richardjacksonxyz 2 หลายเดือนก่อน +3

    GENUINELY just sat down to try incorporating some partial classes into my work and here we are!
    *puts kettle on*

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      Great!

  • @daniero
    @daniero 2 หลายเดือนก่อน +7

    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

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +4

      Yep. Since they get combined together, the attributes get combined as well.

  • @hebreux11KC
    @hebreux11KC 2 หลายเดือนก่อน +2

    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

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      Thank you!

  • @MichaelChibaka
    @MichaelChibaka 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      Yep, that's a good use for them.

  • @torrvic1156
    @torrvic1156 2 หลายเดือนก่อน

    Thank you so much for your insight, Mr. Corey! You reminded me of how WinForms works in this video.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      You are welcome.

  • @wilbertholkema
    @wilbertholkema 2 หลายเดือนก่อน +3

    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

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      Thanks for sharing!

  • @platoh
    @platoh 2 หลายเดือนก่อน

    I'm digging these short form dives

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      Great!

  • @Rick-tg4oy
    @Rick-tg4oy 2 หลายเดือนก่อน +3

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      Yep, that's a good use of partial classes.

    • @rikudouensof
      @rikudouensof 2 หลายเดือนก่อน

      yes. It really helps with the MVVM. it also means one will have to also use mvvm package on xaml for some special bindings

  • @OeHomestead
    @OeHomestead 2 หลายเดือนก่อน +3

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      Interesting.

    • @samnihao6943
      @samnihao6943 2 หลายเดือนก่อน

      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

  • @bezimienny5
    @bezimienny5 2 หลายเดือนก่อน +2

    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

    • @keyser456
      @keyser456 2 หลายเดือนก่อน +6

      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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      As @keyser456 pointed out, it is really useful today when working with source generation.

  • @ralphtashinganyoni2460
    @ralphtashinganyoni2460 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +3

      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.

  • @JimmyGee
    @JimmyGee 2 หลายเดือนก่อน

    Thanks! I like those short explainers.

    • @jonathancumini6183
      @jonathancumini6183 2 หลายเดือนก่อน

      Could be way shorter in my opinion

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      I'm glad you enjoy them.

  • @TheJessejunior
    @TheJessejunior 2 หลายเดือนก่อน

    very good, I've been looking for this answer for long :)

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      I am glad it was helpful.

  • @clebersondot-net
    @clebersondot-net 2 หลายเดือนก่อน +1

    thanks tim!

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      You are welcome.

  • @benjaminshinar9509
    @benjaminshinar9509 2 หลายเดือนก่อน

    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?

  • @ChrisWard74
    @ChrisWard74 2 หลายเดือนก่อน

    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?

  • @pedrobettt
    @pedrobettt 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +2

      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.

  • @holger_p
    @holger_p 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      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_p
      @holger_p 2 หลายเดือนก่อน

      @@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 ).

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      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.

  • @refactorear
    @refactorear 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      Thanks for sharing!

  • @OniFeez
    @OniFeez 2 หลายเดือนก่อน

    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++?

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      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.

  • @jasoncox7244
    @jasoncox7244 2 หลายเดือนก่อน

    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).

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      That's not why it is there, but I'm glad it works for you.

    • @jasoncox7244
      @jasoncox7244 2 หลายเดือนก่อน

      @@IAmTimCorey I know~ but I appreciate being able to `abuse` ... ahem... find the save use edges of the grey areas... of features ;) lol

  • @rik2243
    @rik2243 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      Yep, that’s great!

  • @princefowzan
    @princefowzan 26 วันที่ผ่านมา +1

    3:20 when creating the instance, why did you stop with new(), don't you have to add new Demo()?

    • @IAmTimCorey
      @IAmTimCorey  25 วันที่ผ่านมา +1

      Not in the later versions of .NET. The compiler infers the value based upon the type you specified at the beginning.

  • @antoniusivan8767
    @antoniusivan8767 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      Great!

  • @thespecialistt215
    @thespecialistt215 2 หลายเดือนก่อน

    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?

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +2

      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.

  • @DadDoesCode
    @DadDoesCode 2 หลายเดือนก่อน

    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
      @holger_p 2 หลายเดือนก่อน +1

      There are no "partial classes". It's one class, just split in multiple files.

    • @DadDoesCode
      @DadDoesCode 2 หลายเดือนก่อน

      @@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_p
      @holger_p 2 หลายเดือนก่อน

      @@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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +3

      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.

  • @ThunderChasers
    @ThunderChasers 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +2

      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.

    • @ThunderChasers
      @ThunderChasers 2 หลายเดือนก่อน

      @@IAmTimCorey you're the man! Thanks for the reply.

  • @ChillGuyz
    @ChillGuyz 2 หลายเดือนก่อน

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      Thank you!

  • @kevinnowicki3830
    @kevinnowicki3830 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      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.

    • @nonameno2006
      @nonameno2006 2 หลายเดือนก่อน

      Just use git

  • @Arvydas1987
    @Arvydas1987 2 หลายเดือนก่อน

    explainned

  • @jayrigger7508
    @jayrigger7508 2 หลายเดือนก่อน

    I use a name like Demo.Other.cs based off the class it is partial of

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน

      That can work.

  • @SteffenSkov
    @SteffenSkov 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +1

      Yeah, this definitely isn't for splitting up your code because your code is too big. That's not why it exists.

  • @sanjeevsjk
    @sanjeevsjk 2 หลายเดือนก่อน +1

    1st

  • @CSharpProdigy
    @CSharpProdigy 2 หลายเดือนก่อน

    3rd

  • @jouchiwinchester2666
    @jouchiwinchester2666 2 หลายเดือนก่อน

    2nd

  • @月牙天衝-y7u
    @月牙天衝-y7u 2 หลายเดือนก่อน

    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.

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +7

      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.

    • @月牙天衝-y7u
      @月牙天衝-y7u 2 หลายเดือนก่อน

      @@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

    • @IAmTimCorey
      @IAmTimCorey  2 หลายเดือนก่อน +5

      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.

    • @月牙天衝-y7u
      @月牙天衝-y7u 2 หลายเดือนก่อน

      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.

  • @droberts7942
    @droberts7942 25 วันที่ผ่านมา

    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?

    • @IAmTimCorey
      @IAmTimCorey  20 วันที่ผ่านมา

      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.