C
Join ServerC#
help
❔ ✅ printing array like this [7, 14, 21, 28, 35] inside of brackets
RRyan-T141212/24/2022
.

RRyan-T141212/24/2022

RRyan-T141212/24/2022
output: System.Int32[]
returned arrayCcanton712/24/2022
You can use a
for
or foreach
loop, or string.Join
Ccanton712/24/2022
So
"[" + string.Join(", ", myArray) + "]"
, or use string interpolation $"[{string.Join(", ", myArray)}]"
RRyan-T141212/24/2022
idk about string interpolation
RRyan-T141212/24/2022
is it like list comprehension in python
EEro12/24/2022
No, it's like string interpolation in python
EEro12/24/2022
f"My value: {myValue}"
RRyan-T141212/24/2022
hmm i got it
RRyan-T141212/24/2022
thx
RRyan-T141212/24/2022
but where is the index numn
RRyan-T141212/24/2022
{0}
RRyan-T141212/24/2022
and why there is a $ symbol
Ccanton712/24/2022
C# uses
$"..."
for string interpolation, just like python uses f"..."
Ccanton712/24/2022
There's no
{0}
because string interpolation doesn't use {0}
RRyan-T141212/24/2022
so if i type print(myArray) python making this automatically
Ccanton712/24/2022
Indeed, and C# doesn't
RRyan-T141212/24/2022
cool
RRyan-T141212/24/2022
// Creating Objects
Problem test1 = new Problem();
var result = test1.ArrayOfMultiples(5,7);
// Printing Variables
Console.WriteLine(result);
// Class
class Problem
{
// Method
public string ArrayOfMultiples(int num, int length)
{
int[] myArray = new int[length];
for( int i = 0; i < length; i++ )
{
myArray[i] = (i+1) * num;
Console.WriteLine(myArray[i]);
}
return $"[{string.Join(", ", myArray)}]";
}
}
RRyan-T141212/24/2022
which one is the most common
Ccanton712/24/2022
No idea. Use whichever you think is more readable
RRyan-T141212/24/2022
allright
RRyan-T141212/24/2022
!close
AAccord12/24/2022
Closed!
AAccord12/25/2022
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.