Reputation: 73
Very new to C# and completely new to C# in VS code. Im not sure if there is an issue with my code or the application file set up, in either case the return value I receive is System.Int32[], not the actual array contents.
I created a console project - dotnet new console -n "algos"
I added a solution file - dotnet new sln -n "algorythems_solution"
I added project to solution - dotnet sln algorythems_solution.sln add ./algos/algos.csproj
To run the program I have used f5 and - dotnet run
using System;
namespace algos
{
class sortingAlgorythems
{
static void Main()
{
int[] test = {-4,5,10,8,-10,-6,-4,-2,-5,3,5,-4,-5,-1,1,6,-7,-6,-7,8};
int[] sorted = bubbleSort(test);
Console.WriteLine(sorted.ToString());
}
public static int[] bubbleSort(int[] array)
{
bool isSorted = false;
int toValue = array.Length - 1;
int fromValue = 0;
while (isSorted == false)
{
isSorted = true;
int cnt = 0;
for (int i = fromValue; i < toValue; i++)
{
if (array[i] > array[i+1])
{
cnt = i;
int temp1 = array[i];
int temp2 = array[i+1];
array[i] = temp2;
array[i+1] = temp1;
if (isSorted == true)
{
isSorted = false;
if (i > 0)
{
fromValue = i -1;
}
}
}
}
toValue = cnt;
}
return array;
}
}
}
Any insights into what my issue may be are greatly appreciated.
Upvotes: 0
Views: 105
Reputation: 7354
Here's an example generic extension method. Note it must be defined in a public static class
.
public static void WriteToConsole<T>(this IList<T> list)
{
for(int i = 0; i < list.Count; ++i)
{
Console.WriteLine(list[i]);
}
}
Can be used like this:
int[] arr;
arr.WriteToConsole();
Upvotes: 2
Reputation: 45262
You have incorrectly assumed what ToString()
does. The default behavior is to simply display the type of the object (though it is overridden for many types to display something more useful).
Here's a simple solution for you:
int[] sorted = bubbleSort(test);
for(int i = 0; i < sorted.Length; ++i)
{
Console.WriteLine($"{i}: {sorted[i]}");
}
Upvotes: 2