Monster shadow

VIDEO

C# Extension Methods

Description:
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: https://www.patreon.com/tarodev ========= 🔔 SUBSCRIBE: https://bit.ly/3eqG1Z6 🗨️ DISCORD: https://discord.gg/GqeHHnhHpz ✅ MORE TUTORIALS: https://www.youtube.com/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
ADD A COMMENT

Caleb
NICE
Mir Tanvir Ahmed
I would love to see you as the game dev YouTube channel 💜 you are just awesome 🤪
VEOdev
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 ?
Miroslav Valev
What code editor are you using VS Code?
STVS
I just found out your channel... All I have to say: This is a goldmine!
WarlikeCracker
Спасибо за годный контент
Tarodev
You're welcome :)
T. Zacharias Koch
You are actually someone who knows what he is talking about! Thanks!
Bruno Drago
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)
SFP
how does your project start so fast lol
SFP
@Tarodev I had a feeling you had one video about that, thanks :)
Tarodev
Go to my channel and look for 'enter play mode faster'
ГеймДеб
666th like
Tarodev
🤘
Benjamin Lehmann
Good job
DAcKey
Nice
Gêm Đép 2
That's so perfect for tutorial, thanks
Jean-Michel HOUBRE
Very clear.
James Scott-Nicholson
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
@James Scott-Nicholson it's the time to change! Start doing it and you will kick yourself for not doing it sooner.
James Scott-Nicholson
@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
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?
James Scott-Nicholson
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.
88dejw
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
Yeah sorry about that. I realize I didn't zoom when I started editing. I tried zooming the video but it looked bad 😢
Elvin Gamer
I am a C# dev but I clicked purely because of the Clash of Clans font so take that for what you will
Tarodev
I love it 😅
Pepijn van der Linden
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!
Fırat Yıldırım
My new favorite channel
SEE ALL COMMENTS


Transcript:

hello my lovelies so today i'll be talking about extension methods
extension methods are a way to add methods to a class which you could
potentially not even own and then the methods will act like any
other normal instance method but let's jump right in so you know what the hell
i'm talking about so uh i've just got this little demo seen here so i've got
these three islands and obviously when we hover there's this uh light up effect
it's not actually a light up effect it's changing the alpha from something like
point three to one and then when we click on it we've also got these little
level selections which also light up around the same effect all right so
let's look at the code all right so i've got a reference to our
renderer i've got a on mouse enter an on mouse exit and i've got this little fade
function here where we take in the alpha so it's either you know 0.33 or it's one
now this is such a common occurrence wouldn't it be nice if we could just
call renderer.fade but unfortunately we don't own the sprite renderer class so
we can't actually add it as an instance method directly on the class
but we can do something pretty much just as good so let's take this fade function
and go across to my static class right it's just a normal static class
and let's just plonk that in and we'll change it to a public static method
and as we we've changed context here we don't actually have a reference to this
sprite renderer so we have to add that in sprite renderer
renderer beautiful
so now we've got this method it's a static method and we could use it in the
island and we could use it on those level uh icons as well so right here
let's just say uh extensions dot fade and then this will take in the renderer
and then this is uh on mouse enter so one and then we can copy that and put
that here and just send in 0.33 and uh that would work just like normal
as you'd expect just press play and we can see that it fades just nicely
cool uh but this is not actually an extension
method i know i've put it in an extensions class but this is just you
know this is just calling a normal static method uh so let's actually make
it an extension method all we have to do to turn it into an extension method is
to prefix this first argument here with the this keyword so what this keyword is
doing uh is it saying you can now call this fade function
as if it's actually a function on this parameter here the the renderer so if we
go back to uh islandscript we'll actually say that writer is suggesting
something it's saying uh to extension method evocation so it's saying hey
would you prefer to call the uh extension method version of this and yes
indeed we would so there we go now it's like our fade
function is actually an instance method of the sprite renderer class
very very cool very easy very nate
and of course it's predictable
but uh it's all working so that's how easy it is to set up an
extension method just put it in a static class and uh prefix the first argument
with this keyword and you've got yourself an extension method so i'll
show you a few more examples uh
here we've got a list of hero names and i'm sure you've done this a million
times before but you need a random element from this list so let's
say heroes random range from zero to
heroes counts right so grab a random element from this array and let's print
the hero name but let's say you need to
constantly grab randoms right from this list that list all over the place i know
i do constantly grabbing randoms so let's
make an extension method of this signature so that we can easily
grab a random so down here let's make a public static and this will return a
string and this will be let's just call this
rand this will take in a list of string and
let's say return list
random range 0
to the list dot count call and then we can go back to our
examples and we can just say heroes dot rand
and now we've got uh the exact same functionality but super easy to grab and
now you can use rand all over the place and while i'm at it
you've probably been using extension methods all the time with uh link like
for example where and first they're all extension methods
sorry now you can just make your own extension methods
but this was a little bit nasty uh we can really only use this on list
strings so let's actually make this generic
like this oops like that and then this list will be of
type t so now we don't actually care what what
the uh value type in this list is and actually we can actually make this an
eye list uh to give you even more flexibility so now uh you can use this
rand function on uh any type of list to easily grab a random element
cool bananas and now instead of just printing the name uh which we should
probably do at least once right test behavior
and let's just chuck on extra examples and then press play
cool hercules and then hercules again holy moly tara dev
hercules taradev thanos cool excellent i just wanted to get all of
them for my ocd don't actually have ocd but maybe it helped one of you guys uh
okay so instead of just printing the name let's
uh actually mutate the name let's let's do something so in our extension methods
let's make a public static static
and this will return a string and let's just um
what can we do with split string or something let's take in a string input
and then we will return um input dot
split and then we'll also
string dot join and we'll use a space there
and then put that there so then that should
split it and we'll do two split string so now we've made an override for
sorry now we've made an extension method for string
um so then if we go back to unity and press play
it does the exact same thing as before uh
probably not split um to char array there we go that should work
there we go so now we're mutating a string with a super simple extension
method uh that seems like it's actually just an instance method on string
cool so there is one last thing that i want to show you uh and it's a gotcha
it's it's uh smacked me in the face before so let's say we've got a class
here and this will be a number class
and this will hold a arbitrary number um and that will be this one and then
this will have a function which will return an ins and this will be get
number and this will just just return the
number cool
now let's say uh we don't own this class it's it's someone else's class it's
maybe in from a library or uh it's it's compiled into a dll or whatever
uh let's say we want to extend this class
now so let's make a uh a static class here and i'm just going to put it here
instead of extensions just because uh it's all on one screen this will be
called the uh number extensions
and in here we're going to have a function
that's also going to return an int and it's also going to be called get number
right and this is an extension method so we need to take in a disk a number class
number class and i'm just going to completely ignore
this number class right it's my extension method i can do whatever the
hell i want to do with it so i'm just going to return a
new number and now if we come up here and we make a
an instance of this number class equals new number class and then we say
uh prince number class dot get number
and go back to unity and press play we'll say that at prince 69 so it is
ignoring uh brand spanking new extension method
and that's because the compiler takes precedence over instance methods uh over
extension methods so first it will come down it will say all right is there a is
there a signature that returns an int and it's called get number
no there might not be all right well then in that case let's actually go look
for a uh an extension method that doesn't mean that extension methods are
slow by no means are slow they're blazingly fast so don't not use them
because you think there's a performance issue there is not uh so yeah that's
that's a gotcha that you should be aware of because it has punched me in the
bowls before um
so yeah i hope you found this video helpful and entertaining
i'm going to pop a whole bunch of uh useful extension methods up on the
screen that i use all the time so grab and steal some of those and
start using them yourself if you enjoyed this video give it a
thumbs up subscribe and i'll see you on the next one bye