C# Method Overloading in Unity! - Intermediate Scripting Tutorial

preview_player
Показать описание
Watch this video in context on Unity Learn:

Overloading a method allows you to create different methods with the same name. The result is that you can provide a range of functionality without creating a lot of clutter. In this video you will learn how to create and use overloaded methods.
Рекомендации по теме
Комментарии
Автор

Transcript:

Overloading is a process by which you can give a single method multiple definitions. This means that you can use the same method name to do two different things. Let's say you needed a method to add things together. You could create an AddNumbers() method that would be responsible for adding two numbers together. Adding strings together, however, works differently; so you would need a new method called AddStrings(). This works fine, the problem is that now you need to remember two different method names that in essence do the same thing. A better way would be to overload a method named Add() to work with either numbers or strings. Here we have a method named Add() that reads in two numbers and returns a number. Every method has what is called a signature. The signature is comprised of a method's name and parameter list. No two methods can have the same signature within the same scope. We overload methods by giving a new method the same name, but a different signature. Using our previous example we can overload this Add() method to create a new method that adds strings. Notice that our new Add() method has the same name, but a different parameter list. Since it is a different signature, this is allowed. In some other class when we try to access the Add() method, we can see that there are two versions of it. The correct version will be chosen, based on the arguments you pass into it. By passing in two numbers, the method that adds numbers will run. Likewise, if you pass in two strings, then the method that adds strings will run. One of three things will happen when the system tries to decide on the correct version of an overloaded method to run. First, if there is an exact match to the parameters passed in, then that version of the overloaded method will run. If there isn't an exact match, the system will look at all of the possible matches and will choose the one which will require the least amount of conversion. Finally, if there are no possible matches, or multiple versions that require the same amount of conversion, then an error is thrown.

rajaspydey
Автор

Thanks for these videos. Really very helpful for beginners.

UGuruz
Автор

I'm looking to add an overload to an invoke. I have no idea how to do so and no one talks about it.

afish
Автор

What if I try to "Add" a string and and "Int", what would be the result ? Would it be a conversion of the int to a string ? Or it will return me an error ?

DiversityCraft
Автор

wish i could add overloads to existing classes, like quaternion, mathf or list... as easily as this...

MarekNowakowski
Автор

seems like java and csharp are almost the same.

erroashivudhi
Автор

Let me save you some 2 mins of your life, just create 2 methods with the same names but with different data types.

piztech
Автор

Is it a race? Why talk so fast? Think a little to foreign listeners, thank you!

jean-michel.houbre