✅ what will be the default value of the `params int[] value` if i did not pass it while calling M
what will be the default value of the
params int[] value
if i did not pass it while calling method , do this will be trated as an empty array of ints
14 Replies
yes, it will pass an empty array
i want to ask another question regarding optional parameter , the below are a way to make parameters optional using method overloading , Is doing it like below a best practice or i should re-implement every thing in the overloaded method
I would just put params on the second method and remove the first one
yes thats correct but for now i am learning haw can i make it using the method overloading
Well, but the calling patterns are different
You wouldn't be able to write
Sum(1, 2, 3)
without paramsyes of course if i use params it better because i can spred all my numbers easly but on the other i have to pass array
Unless you add a lot of different overloads
for the first method this is an overloaded version that calls the secound one inside it is that a best practice o i have to re-implement there
It's often how overloads are implemented, yes
i never used overloading like his i always re-implement the methods but i neve used that technique ,but any way its somthing that is added to my knoladge
but i dont thing the re-implimintation is wrong
It depends. You usually don't want to have multiple copies of the same code
Harder to maintain long-term
yes that correct that i cas i want it to be the same correct but if i have diffrent implementations like adding more if condation for checking somthing (this is an example ) then i can re- implement
correct?
I would generally encourage you to not have different implementations
Also, just to mention: this isn't a great example to learn overloading on, because you wouldn't use overloading for this method in the real world
i just leanet the basics of overloading usng this example because overloading depend on the application that i am creating
i mean i leaned how to overload a method inside a class but implimintations are depends on the application
@333fred @reflectronic thank you