Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Which language to use: C# or VB.Net
Message
 
À
17/01/2002 07:23:36
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00605780
Message ID:
00606031
Vues:
23
>Overloading: What C# has that VB.Net does not is operator (+, -, *, etc) overloading.
>If you don't like what the plus (+) operator does, you can overload it in C#.

I am a Billiards player and my strength is defined by the amount of caramboles made divided by the amount of innings.

I have a data type 'Game' that has the amount of caramboles made and the amount of innings. From it the strength can be calculated and stored. I also store the GamesPlayed there to know how many games have been played.

To add 2 such datatypes, I have to add the caramboles together and the innings together and divide that result to get the new strength. The same for GamesPlayed.

Without operator overloading I couldn't add those 2 datatypes together, I had to create a new method like AddGame(Game Newgame) to add a new game to a game.
And use this code:
TotalGames.AddGame(Game Game1);
TotalGames.AddGame(Game Game2);
But with operator overloading I can change the behaviour of the operator.
If I have this:
public static Game operator +(Game g1,Game g2)
I have a method that gets 2 parameters and should return the result. I do my stuff in there.

Now I can do this:
TotalGames = Game1 + Game2
and the overloading kicks in.

Even if you use operator overloading, the same functionality should be available, using conventional methods, otherwise it can't be used in languages that don't support operator overloading.

For more info, see:
http://www.csharphelp.com/archives/archive135.html

hth,
Frank Camp
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform