C
C#2y ago
Ryan-T1412

❔ ✅ printing array like this [7, 14, 21, 28, 35] inside of brackets

.
13 Replies
Ryan-T1412
Ryan-T1412OP2y ago
Ryan-T1412
Ryan-T1412OP2y ago
output: System.Int32[]
output: System.Int32[]
returned array
canton7
canton72y ago
You can use a for or foreach loop, or string.Join So "[" + string.Join(", ", myArray) + "]", or use string interpolation $"[{string.Join(", ", myArray)}]"
Ryan-T1412
Ryan-T1412OP2y ago
idk about string interpolation is it like list comprehension in python
ero
ero2y ago
No, it's like string interpolation in python f"My value: {myValue}"
Ryan-T1412
Ryan-T1412OP2y ago
hmm i got it thx but where is the index numn {0} and why there is a $ symbol
canton7
canton72y ago
C# uses $"..." for string interpolation, just like python uses f"..." There's no {0} because string interpolation doesn't use {0}
Ryan-T1412
Ryan-T1412OP2y ago
so if i type print(myArray) python making this automatically
canton7
canton72y ago
Indeed, and C# doesn't
Ryan-T1412
Ryan-T1412OP2y ago
cool
// 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)}]";
}
}
// 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)}]";
}
}
which one is the most common
canton7
canton72y ago
No idea. Use whichever you think is more readable
Ryan-T1412
Ryan-T1412OP2y ago
allright !close
Accord
Accord2y ago
Closed! 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.
Want results from more Discord servers?
Add your server