+ 1

C# Overloading Example showcases confusing returns

Here is the code I'm working with: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Score tm1 = new Score(2, 3); Score tm2 = new Score(4, 2); Score finalScores = tm1 + tm2; Console.WriteLine("Round 1: " + finalScores.round1Score); Console.WriteLine("Round 2: " + finalScores.round2Score); } } class Score { public int round1Score { get; set; } public int round2Score { get; set; } public Score(int r1, int r2) { round1Score = r1; round2Score = r2; } //your code goes here public static Score operator+ (Score a, Score b) { int r1 = a.round1Score + b.round1Score; int r2 = a.round2Score + b.round2Score; Score res = new Score(r1, r2); return res; } } } It's fully functional, it was the answer to a question on the C# intermediate course...but although I wrote it I don't quite understand it. How can an object be "returned" as it is here: >Score res = new Score(r1, r2); >return res; Idk why it's confusing me so much, it just is...usually when something is returned it's a variable. Like return x or something like that. But an object....what does that really mean? What does it entail? What can you do now that a method, in this case the overloaded "+" is returning an object, especially one that has 2 parameters?

19th Oct 2023, 5:04 AM
21kHzBANK21kHZ
21kHzBANK21kHZ - avatar
3 Réponses
+ 3
Imagine for a second how the default addition operator might be implemented for regular int numbers. Maybe something like this (simlified for the example, not the actual code): public static int operator+ (int a, int b) { int result = a + b; return result; } What your code does is very similar. It describes the logic how two Score objects can be added. A Score encapsulates two numbers, and the addition creates a new Score object, using the both numbers from both sides of the addition in the process. Don't be confused that the return type is a custom class. Even the built-in standard data types are actually classes or structs, which can have normal and static methods. See the documentation of int for example: https://learn.microsoft.com/en-us/dotnet/api/system.int32?view=net-7.0 So the arguments and the return types of a method can be any type, either built in or defined by you or defined in an external library.
19th Oct 2023, 1:06 PM
Tibor Santa
Tibor Santa - avatar
+ 3
Yes indeed, the possibilities are endless. But at least the benefit of a statically typed language such as C#, is that the function signature informs you exactly which types are expected / allowed as parameter and what is returned. So the types also serve as a kind of documentation. In contrast, a dynamic language such as Javascript or Python will not tell you in the function signature, what kind of data you should pass in and what is given back, you actually have to look up in the language specification and read it.
19th Oct 2023, 6:21 PM
Tibor Santa
Tibor Santa - avatar
+ 2
@Tibor Santa Thanks for explaining that. It opens the door to a lot of possibilities so it's not all quite clear to me without experimenting, but I think I understand a little better.
19th Oct 2023, 6:07 PM
21kHzBANK21kHZ
21kHzBANK21kHZ - avatar