C# Extension Methods

Поділитися
Вставка
  • Опубліковано 22 лип 2024
  • Extension methods grant you the ability to extend classes you might not own without deriving from the class or modifying the original type. Add methods to any class and access them like you would any native instance method.
    Perfect for extending base unity types, like Vector and SpriteRenderer.
    ❤️ Become a Tarobro on Patreon: / tarodev
    =========
    🔔 SUBSCRIBE: bit.ly/3eqG1Z6
    🗨️ DISCORD: / discord
    ✅ MORE TUTORIALS: / tarodev
    0:00 What are extension methods?
    1:00 Our first extension method
    3:10 Extending the list class
    5:43 Extending the string type
    7:00 Extension method call priority
    9:15 Useful extension methods

КОМЕНТАРІ • 145

  • @Tharky
    @Tharky 2 роки тому +49

    ✔ Very very cool
    ✔ Very easy
    ✔ Very neat

  • @Tarnil97
    @Tarnil97 2 роки тому +46

    One thing I'd add is that you can use the 'ref' keyword on the extended argument. Can be useful for, for example, mutating a collection in place

  • @SpicyMelonYT
    @SpicyMelonYT 2 роки тому +44

    Every single interesting thing that I have heard about recently you make a video on like a week later. You are literally predicting what I want to learn next. Legend!

    • @Tarodev
      @Tarodev  2 роки тому +35

      I'm just outside your window

    • @SpicyMelonYT
      @SpicyMelonYT 2 роки тому +3

      @@Tarodev There actually was a person outside my window a few hours before I wrote that. And it wasn't my neighbors cause they are always loud and talking when they go by. Creepy bro! lol

    • @DmpDk
      @DmpDk 2 роки тому +4

      @@SpicyMelonYT Ah no worries, it's normal too see programmers foraging outside in ya garden around this time of year, just give him a cuppa of joe and he will bugger of on his own. else, sprinkle your garden with invitations to sprint meetings and mails from projectmanagers / IdeaGuys with "the next big thing" to keep your garden guaranteed programmer free

    • @SpicyMelonYT
      @SpicyMelonYT 2 роки тому +1

      @@DmpDk LMAO will do mate!

  • @ershadhussain8196
    @ershadhussain8196 2 роки тому +38

    you are slowly becoming my favorite game dev tutorial channel. keep up the useful content!

    • @CyCloNeReactorCore
      @CyCloNeReactorCore 2 роки тому +5

      hes been mine since the video about generics

    • @Tarodev
      @Tarodev  2 роки тому +6

      I appreciate it :) Hopefully I can climb to #1 of your list... It is now my mission!

    • @Tarodev
      @Tarodev  2 роки тому +5

      Thanks a bunch man :) Glad I can deliver enjoyable content for you

  • @kubstoff1418
    @kubstoff1418 2 роки тому +11

    I'm amazed by the simplicity to usefulness ratio!

  • @hamsterbyte
    @hamsterbyte Рік тому

    This was a rollercoaster of me shaking my head and nodding.
    Makes a static class called extensions, adds a method to it and calls if from the class. *face palm*
    Immediately turns the method into a proper extension method. Redeemed.
    Makes an extension method to return a random from a list of strings. *face palm*
    Immediately makes the method generic. Redeemed.
    You're a true legend.

    • @Tarodev
      @Tarodev  Рік тому +1

      Gotta keep you guessing my man

  • @dabekka
    @dabekka 2 роки тому

    your channel is a goldmine for game programmers!

  • @JesusW4y
    @JesusW4y 11 місяців тому

    Absolute favorite channel for intermediate level programming!

  • @rafarodriguez4765
    @rafarodriguez4765 2 роки тому +1

    After 4 years of using C# I learn something useful in each video. Thanks

  • @bunggo9914
    @bunggo9914 2 роки тому +1

    I always learn an entirely new thing about C# in this channel

  • @meuix3218
    @meuix3218 2 роки тому +1

    bro this is INSANE !!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • @vayctorkeesh5393
    @vayctorkeesh5393 2 роки тому

    ty for putting those screenshots of extension methods at the end, really neat !

  • @stvs9722
    @stvs9722 2 роки тому

    I just found out your channel... All I have to say: This is a goldmine!

  • @ewwitsantonio
    @ewwitsantonio 2 роки тому +6

    Man, I love this channel. You are creating content right in my level of skill. I can follow everything easily, but you keep on showing me things that are new/useful. Thanks for the vid!

  • @ZacIRL
    @ZacIRL 2 роки тому

    You are actually someone who knows what he is talking about!
    Thanks!

  • @Alljoker2
    @Alljoker2 2 роки тому +1

    Very nice! I hope to build my toolbox of useful extension methods. Thanks for the video! Gave it a big like.

  • @ivo435
    @ivo435 Рік тому +2

    I am still pretty new to game development (I've been doing it for about 9 months now). This channel is absolute gold. I can't tell you how many times I have thought to myself "I wish this was a method I can call". For me, the hardest part is keeping things nice and tidy (I tend to make waaay to many scripts and I start getting lost in them). This is definitely a neat trick and I will 100% use it in the future. Thank you!

  • @yusufhabib3507
    @yusufhabib3507 Рік тому

    you are my fav game dev channel plz keep making vids like this

  • @pliniomourao
    @pliniomourao 2 роки тому +1

    Nice, simple and easy.
    Thank you for this.

  • @jsn4591
    @jsn4591 2 роки тому +1

    Great video! To the point and a clear bunch of examples. Thanks for adding something new to my toolbox. Subbed and looking forward to checking out your other vids.

  • @narkid89
    @narkid89 2 роки тому +1

    As always, good stuff!

  •  2 роки тому

    Love your videos, thanks for sharing!

  • @datablob
    @datablob 2 роки тому +1

    Awesome stuff, Taro. I laughed so hard during the checklist of coolness and neatness, well done! :D

    • @datablob
      @datablob 2 роки тому

      I particularly also liked how you dove into multiple practical demos of the concept!

  • @kyleme9697
    @kyleme9697 Рік тому +1

    Time to rewrite all my code again !! That generics thing was really interesting, I need to look into that some more.

  • @09mrbubbles
    @09mrbubbles 2 роки тому +1

    Was about to skip this since I thought I already knew what extension methods were. Turns out I did not. It's absurd how much a ten minute video can help you in your career. Thanks a bunch!

  • @mementomori7160
    @mementomori7160 2 роки тому +1

    I had no idea about this, it'll be very usefull

  • @JoschinoDev
    @JoschinoDev 2 роки тому +3

    *this* is incredible. Time to rewrite my helper class

  • @fratyldrm1749
    @fratyldrm1749 2 роки тому

    My new favorite channel

  • @libberator5891
    @libberator5891 2 роки тому

    this.
    this is excellent
    And so are you, my dude

  • @lukastomasek8038
    @lukastomasek8038 2 роки тому +1

    Great Tutorial as always!

  • @mirtanvirahmed3868
    @mirtanvirahmed3868 2 роки тому

    I would love to see you as the game dev UA-cam channel 💜 you are just awesome 🤪

  • @eduardopoffo9764
    @eduardopoffo9764 2 роки тому +1

    Wonderful tutorial, had no idea this existed and now I may never stop using that hahaha
    +1 sub

  • @Mariofan7
    @Mariofan7 2 роки тому

    Thanks a lot for the video. Extension methods indeed seem Very very cool, very easy and very neat

  • @TheKr0ckeR
    @TheKr0ckeR 2 роки тому +3

    great guide, thanks

  • @MandyLeeYT
    @MandyLeeYT 2 роки тому

    Love your little check list ♥

  • @user-cu5ky8qz2n
    @user-cu5ky8qz2n 2 роки тому +3

    Thank you very much, your tutors are wonderful

  • @michalski9141
    @michalski9141 2 роки тому

    I’ve always wanted to know how to make those, great tutorial thank you

  • @null21235
    @null21235 2 роки тому +1

    That's so perfect for tutorial, thanks

  • @Etredal
    @Etredal 2 роки тому +1

    Wow I'm going to use this more!

  • @fleity
    @fleity 2 роки тому +6

    I noticed it in this video and a bunch of other video of you as well. Your Unity compiles and enters playmode ridiculously fast. For playmode enter I know you can disable scene and domain reload but I have run into problems with that once before. I would love a video on how to improve the speed of entering playmode (and writing your scripts in a way that works with skipping domain reload) and improving compilation speed to the level it works for you (I am already using asmdefs but even in a project with only a single script it is not as fast as yours). Thanks a bunch for the good content :).

    • @Tarodev
      @Tarodev  2 роки тому +6

      This would be a few things:
      1. This is a fresh project, so not many scripts
      2. I edit a bit of the load out so you don't have to wait for it ;)

    • @fleity
      @fleity 2 роки тому

      @@Tarodev hehehe well okay that obviously works too :D

    • @datablob
      @datablob 2 роки тому

      @@fleity the hollywood style of development :D

    • @somethingdarkinside
      @somethingdarkinside 2 роки тому

      @@Tarodev big brain 🧠

  • @DJnoratos
    @DJnoratos 2 роки тому

    Really cool indeed ! I code in C# for 3 years now and did not know about this

    • @Tarodev
      @Tarodev  2 роки тому

      Programming is such a vast topic with so many concepts. You'll always discover new goodies 😊

  • @PepijnLinden
    @PepijnLinden 2 роки тому +3

    While watching this video I thought to myself: "This is neat! I should really subscribe to this guy!"
    But apparently I was already subscribed because of that amazing DOTWEEN video.
    Big thanks for spreading your knowledge and keep up the great content!

  • @supendi42
    @supendi42 2 роки тому +1

    Of course Tarodev is inside heroes list

  • @monishdhayalan2552
    @monishdhayalan2552 2 роки тому +1

    i have notifications on for this channel but still no notification, i only look at it when it comes in my recommendation

    • @Tarodev
      @Tarodev  2 роки тому

      A few people have said this 😢

  • @notagamedev5494
    @notagamedev5494 2 роки тому +1

    Thank you!

  • @FacePalmProduxtnsFPP
    @FacePalmProduxtnsFPP 11 місяців тому

    Super cool! Thanks man

  • @nikunev
    @nikunev 2 роки тому

    interesting hero gradation :D so humble that Tarodev

  • @JimPlaysGames
    @JimPlaysGames 2 роки тому

    This is so incredibly useful.

  • @jean-michel.houbre
    @jean-michel.houbre 2 роки тому +1

    Very clear.

  • @chujies
    @chujies 2 роки тому +1

    Very very cool
    Very Easy
    Very Neat!
    Haha love it

  • @AlexBlackfrost
    @AlexBlackfrost 2 роки тому +1

    Wow, this is great

  • @benjaminlehmann
    @benjaminlehmann 2 роки тому

    Good job

  • @alaslipknot
    @alaslipknot 2 роки тому

    ✅ Very Very Cool
    ✅ Very Easy
    ✅ Very Neat

  • @dennisgonzales9521
    @dennisgonzales9521 2 роки тому

    Thanks!!!

  • @NOOB097
    @NOOB097 2 роки тому +1

    Super cool 😎

  • @svendpai
    @svendpai 2 роки тому +1

    Another great video :) still waiting patiently for the knitting one

    • @Tarodev
      @Tarodev  2 роки тому +1

      Can you handle my knitting? Not so sure

    • @svendpai
      @svendpai 2 роки тому

      Only if you can knit my handle

  • @Structed
    @Structed 2 роки тому +1

    You should create a library with your extension methods!

    • @Tarodev
      @Tarodev  2 роки тому

      I've seen a few repos around with a bunch, but yeah you're right there certainly a need for an easy to install package.

  • @ardaozler631
    @ardaozler631 2 роки тому +2

    Amazing tutorial as usual but can you zoom a bit when using rider so mobile users can see easier thank you ❤️

    • @Tarodev
      @Tarodev  2 роки тому +1

      Ah dude in editing I remembered I didn't zoom in and felt bad. I tried to add zoom on every code segment but it looked horrible. Sorry bud >

  • @adamsen2
    @adamsen2 2 роки тому +1

    Hey thank you for your great videos.
    I would like to suggest a video about using events in unity, how to set them up and subscribe to them in another script.
    Best regards

  • @RayHorn5128088056
    @RayHorn5128088056 Рік тому

    This is a very old (I mean ancient) OOP trick but thanks for sharing. 😇

  • @tinyctr7433
    @tinyctr7433 2 роки тому

    实在是太有用了!我怎么没有更早认识你呢!

  • @chawlahimanshu6219
    @chawlahimanshu6219 2 роки тому

    I did the random one before, but used IEnumerator instead of IList to make it available for other data types as well

    • @Tarodev
      @Tarodev  2 роки тому

      A bit more difficult with IEnumerable as it doesn't offer count. But still doable

  • @lewaplay
    @lewaplay 2 роки тому +1

    Nice

  • @AetherXIV
    @AetherXIV 2 роки тому +1

    NICE

  • @TheKr0ckeR
    @TheKr0ckeR 2 роки тому

    I would be really glad a tutorial on "ref" "out" keywords

  • @Rovsau
    @Rovsau 2 роки тому +1

    Hercules, Thanos, Tarodev
    I can see it now...
    public static override ThanosSnap() => DoNothing();
    Tarodev saves the world \o/

    • @Tarodev
      @Tarodev  2 роки тому +1

      The power of a programmer

  • @VEOdev
    @VEOdev 2 роки тому

    I have a question .. sometimes when I work with static classes in some mobile devices they doesn't work for some reason .. some said they get garbage collected , is there a way to protect a class from getting garbage collected or is there something I don't know about ?

  • @askcookie1
    @askcookie1 2 роки тому +1

    I'd love to see a video about custom attributes. Commands from Quantum Console are good example
    [Command]
    public void Teleport(Vector3 pos)
    This would automatically grab name of method and register this as command with Vector3 argument.

    • @Tarodev
      @Tarodev  2 роки тому

      Didn't know Quantum Console existed until just now, pretty nice!

  • @jsn4591
    @jsn4591 2 роки тому +1

    Back again after an excursion through your channel. Awesome stuff, you're a great teacher! I hope you keep on going with more intermediate & advanced topics for us peeps who are beyond the basics. One thing I notice you do is use var instead of specifying the type when defining references, is that just a personal taste thing?

    • @Tarodev
      @Tarodev  2 роки тому +1

      Personal taste, but efficiency comes into it. I can tell the type by the declaration and if I can't I'd prefer to hover to see than have every type on display.
      Sometimes you don't know the type right away, so I'd prefer to just start coding than sit there thinking about it. Curious why you prefer specifically typed?

    • @jsn4591
      @jsn4591 2 роки тому

      @@Tarodev thanks for your insight. I wouldn't say I prefer specifically typed, I just learned it that way. Its always seemed unnecessary to me to write the Type twice though, so the var approach is quite appealing.

    • @Tarodev
      @Tarodev  2 роки тому +1

      @@jsn4591 it's the time to change! Start doing it and you will kick yourself for not doing it sooner.

  • @larryd9577
    @larryd9577 2 роки тому +1

    Extension Methods are just syntactic sugar for grouping util functions to their according classes. Which is stupidly useful. Kotlin does it the exactly the same way, JavaScript and TypeScript has it too, just works a little different.
    I'd usually create separate classes for the extension methods named according to the class which I extend on. Like the List extensions go into ListExtensions and the Hero extensions in HeroExtensions and have them all in a name space for alle extension methods.

    • @Tarodev
      @Tarodev  2 роки тому

      I'm glad you don't throw every single method in a monolithic class!
      I was having a convo recently about where people store enums. Some people would chuck all enums in 1 "enums" file. I personally put them below their main class declaration.
      enum HeroType
      will be below the ScriptableHero declaration for example.

  • @bsdrago
    @bsdrago 2 роки тому

    Something could be good is a video about namespaces. I take some 3rd parties scripts and almost no one uses. So, how to use and about how struct them in directories could be a good video =) (Sorry about my english)

  • @ericbeaulieu419
    @ericbeaulieu419 2 роки тому +1

    Unity knows that Thanos isn't a hero

    • @Tarodev
      @Tarodev  2 роки тому

      It seemed that way... We know he is though

  • @therupoe
    @therupoe 2 роки тому +2

    Hey Tarodev! Love the channel, thanks for all of the useful info.
    At the moment I'm learning c# and am just now getting into (maybe) intermediate topics (inheritance, polymorphism, structs and interfaces).
    At what point am I going to know enough to be effective in unity? I'm worried ill jump in too early, brute force stuff and learn the wrong habits. Is there like a minimum level of competency or something? I mostly understand your videos and if nothing else they give me new terms and concepts to research when I'm in over my head.

    • @Tarodev
      @Tarodev  2 роки тому +2

      Interesting question. I would say learn some basic design patterns like the enum-state manager in my GameManager video and then also a normal state manager (infallible code has a good one). If you're making a smallish game, stick with the enum game manager in my opinion.
      With that knowledge I think you're good to just jump in. Making mistakes is learning. You can always join my discord to ask me/community questions if you're unsure about something

    • @therupoe
      @therupoe 2 роки тому +1

      @@Tarodev thanks, man! I appreciate you taking the time.

  • @SFotakos
    @SFotakos 2 роки тому

    Great video.
    A little concerned about what's considered a hero though.
    *stares at Thanos' name

  • @mirtzata
    @mirtzata 2 роки тому

    What code editor are you using VS Code?

  • @88dejw
    @88dejw 2 роки тому +1

    Great explanation! Could you please make the font a little bigger in the editor next time recording? It's a little hard to see watching this on phone

    • @Tarodev
      @Tarodev  2 роки тому

      Yeah sorry about that. I realize I didn't zoom when I started editing. I tried zooming the video but it looked bad 😢

  • @tudorelRo
    @tudorelRo 2 роки тому

    Awesome video as always! Just a small thing at min 6:50 you said that you are mutating a string, but that's false, you are returning a new string and printing it, the "hero" string remains the same . ("String objects are immutable: they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object")

    • @Tarodev
      @Tarodev  2 роки тому

      I talk about string immutability in my previous video. I more meant to the context of the caller, we are 'mutating' the string. Not from a programmatic standpoint but a functional standpoint.
      It's good you mentioned it anyway as others may have the wrong idea 💡

  • @rutchjohnson
    @rutchjohnson Рік тому

    Gold

  • @plixo4712
    @plixo4712 2 роки тому

    whats with the rider licences?

  • @vishalkhombare
    @vishalkhombare 2 роки тому +1

    Tarodev You are such a master!! ..What books you read? recommend to us beginners and intermediates ? Which online resource can teach us all this C# stuff like you do ? Can you recommend somethings / many things additional who really want to get good at this C# programming art. Some day please take time and make a video on this "Recommeded learning resources as per Tarodev". Thanks.

    • @Tarodev
      @Tarodev  2 роки тому +1

      I didn't read any books. I learned by jumping in to make things and just looking stuff up as I got stuck. I would suggest not learning purely via unity tutorials, but focus more on pure c# content.
      Everybody learns differently, I always found hands on the best for me, but books may be best for you 😊 as long as you find the method you choose to be fun and not boring I'd say you're doing it right.

    • @vishalkhombare
      @vishalkhombare 2 роки тому

      @@Tarodev Yes Agreed Boss. Everyone's in that sense. Thanks a lot for your kind reply.

  • @WarlikeCracker
    @WarlikeCracker 2 роки тому +1

    Спасибо за годный контент

    • @Tarodev
      @Tarodev  2 роки тому +1

      You're welcome :)

  • @homemacai
    @homemacai 2 роки тому +1

    it's 420 O' Clock in classroom 69 kids!

  • @TheElvinGamer
    @TheElvinGamer 2 роки тому +1

    I am a C# dev but I clicked purely because of the Clash of Clans font so take that for what you will

    • @Tarodev
      @Tarodev  2 роки тому

      I love it 😅

  • @bini7203
    @bini7203 2 роки тому

    420 69 lol
    This legend sneaked in these holy numbers in a programming tutorial. Niceee XD

    • @Tarodev
      @Tarodev  2 роки тому

      Excuse me they were entirely random

  • @bambuccigames
    @bambuccigames 2 роки тому +3

    How come you enter Play Mode so fast?

    • @ardaozler631
      @ardaozler631 2 роки тому +1

      He has a tutorial on that

    • @Tarodev
      @Tarodev  2 роки тому

      Here it is: ua-cam.com/video/P7cYVg5fAvY/v-deo.html
      Also, ctrl + p is the hotkey to enter

  • @ekekw930
    @ekekw930 Рік тому

    5:38 lmao

  • @znimator
    @znimator 2 роки тому +1

    Why his Unity loading so fast i cant understand bruh
    my takes like 20 seconds to load stuff and then run game

    • @Tarodev
      @Tarodev  2 роки тому +1

      Check out this tutorial my guy ua-cam.com/video/P7cYVg5fAvY/v-deo.html

  • @youcancallmedoggie
    @youcancallmedoggie 2 роки тому +2

    ENGAGING

  • @hryhoriinevmerzhytskyi4299
    @hryhoriinevmerzhytskyi4299 8 місяців тому

    Random int 420 😂 Ok, like ✊

  • @YOSFP
    @YOSFP 2 роки тому +1

    how does your project start so fast lol

    • @Tarodev
      @Tarodev  2 роки тому +1

      Go to my channel and look for 'enter play mode faster'

    • @YOSFP
      @YOSFP 2 роки тому

      @@Tarodev I had a feeling you had one video about that, thanks :)

  • @didntask2438
    @didntask2438 2 роки тому +1

    Hello

  • @user-tp1cj5fc2y
    @user-tp1cj5fc2y 2 роки тому

    666th like

  • @somethingdarkinside
    @somethingdarkinside 2 роки тому

    Good video, really helpful. But unfortunately have to dislike and report because typing braces in the same line is war crime.

    • @Tarodev
      @Tarodev  2 роки тому

      Haha getouttahereeeeee

  • @AndrewNostromo
    @AndrewNostromo 2 роки тому

    public static void DestroyChildrenRecursive(this Transform transform)
    {
    foreach (Transform t in transform)
    {
    t.DestroyChildrenRecursive();
    }
    Object.Destroy(transform.gameObject);
    }

  • @Sticks_Dev
    @Sticks_Dev Рік тому

    Old video, but why do you have a flat extension method? Vector3.ProjectOnPlane() already does exactly what that does if you use Vector3.up as the second argument. To be fair, I do wish project on plane were an extension method by default.

  • @user-ck6eg6fm8t
    @user-ck6eg6fm8t 10 місяців тому

    I Made tihs one =>
    public static T GetRandom(this List ts, int initialInclusive = 0, int finalExclusive = 0)
    {
    if (finalExclusive == 0) { finalExclusive = ts.Count; }
    return ts[UnityEngine.Random.Range(initialInclusive, finalExclusive)];
    }