✅ 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
c#

public static void Sum(int number , int number2,params int[] values)
{
Console.WriteLine(number);
Console.WriteLine(number2);
Console.WriteLine(values is int[]);
}
c#

public static void Sum(int number , int number2,params int[] values)
{
Console.WriteLine(number);
Console.WriteLine(number2);
Console.WriteLine(values is int[]);
}
14 Replies
reflectronic
reflectronic5mo ago
yes, it will pass an empty array
steven preadly
steven preadlyOP5mo ago
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
c#
public static int Sum(int number , int number2)
{
return Sum(number , number2, Array.Empty<int>());
}


public static int Sum(int number, int number2 , params int[] values)
{



int Result = number + number2;

if (values.Length > 0)
{
foreach (int i in values)
{
Result = Result + i;
}
}

return Result;
}
c#
public static int Sum(int number , int number2)
{
return Sum(number , number2, Array.Empty<int>());
}


public static int Sum(int number, int number2 , params int[] values)
{



int Result = number + number2;

if (values.Length > 0)
{
foreach (int i in values)
{
Result = Result + i;
}
}

return Result;
}
333fred
333fred5mo ago
I would just put params on the second method and remove the first one
steven preadly
steven preadlyOP5mo ago
yes thats correct but for now i am learning haw can i make it using the method overloading
333fred
333fred5mo ago
Well, but the calling patterns are different You wouldn't be able to write Sum(1, 2, 3) without params
steven preadly
steven preadlyOP5mo ago
yes 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
333fred
333fred5mo ago
Unless you add a lot of different overloads
steven preadly
steven preadlyOP5mo ago
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
333fred
333fred5mo ago
It's often how overloads are implemented, yes
steven preadly
steven preadlyOP5mo ago
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
333fred
333fred5mo ago
It depends. You usually don't want to have multiple copies of the same code Harder to maintain long-term
steven preadly
steven preadlyOP5mo ago
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?
333fred
333fred5mo ago
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
steven preadly
steven preadlyOP5mo ago
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
Want results from more Discord servers?
Add your server