Home > Mobile >  c# why do I get the same output using classes?
c# why do I get the same output using classes?

Time:12-10

Ive written code that sorts the given input but after it returns the sorted input it will always return the same output. I'm creating console applications using .NET 5.0 (current) in Visual Studio.

When I give as input "Car Apple Banana" it get sorted in words.Sorted()

After that I print out the original input but it seems to be sorted too. I don't know why because I never sort it.

When the input is: "Car Apple Banana"

The output I now get is:

Apple Banana Car

Apple Banana Car

While it needs to be:

Apple Banana Car

Car Apple Banana

Here's the main code:

using System;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;

namespace _10_Words
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input_1 = Console.ReadLine().Split(' ');
            Words words = new Words(input_1);

            Console.WriteLine(words.Sorted());
            Console.WriteLine(words.Normal());
        }
    }
}

Here's the class code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10_Words
{
    class Words
    {
        public string[] Output { get; set; }

        public Words(string[] input)
        {
            Output = input;
        }

        public string Sorted()
        {
            string[] sorted = Output;

            Array.Sort(sorted);

            string sorted_array = string.Join(" ", sorted);
            return Convert.ToString(sorted_array);
        }

        public string Normal()
        {
            string[] normal = Output;

            string normal_output = string.Join(" ", normal);
            return Convert.ToString(normal_output);
        }
    }
}

CodePudding user response:

It seems that you assume that the line

string[] sorted = Output;

copies input to Output, whereupon youc an sort Output while leaving input unchanged.

This is not the case.

Arrays are reference types. As such, when you assign an array like Output to another array variable such as sorted, sorted and Output will reference the same array.

If you want a copy, you will have to iterate over the elements or use a helper method, such as Array.Copy:

sorted = new string[Output.Length];
Array.Copy(Output, sorted, Output.Length);

CodePudding user response:

When you write

string[] sorted = Output;

you are assigning a reference to the array Output to sorted, then you sort iy in place with

Array.Sort(sorted);

but since sorted is just a reference to Output, you are actually sorting Output. A better way is to just print the original array as it comes, then sort it in place and then print it.

CodePudding user response:

string[] sorted = Output;

Array.Sort(sorted);

When you call Array.Sort, this modifies the array you pass into it. Because arrays are passed by reference, sorted and Output refer to the same array, which gets sorted.

In other words, you are changing Output when you sort the elements.

The easiest fix is to make sure that you make a new array, with the same elements as the old one:

// Don't forget to include this at the top of the file
using System.Linq;

string[] sorted = Output.ToArray();

Array.Sort(sorted);

Another solution is to change your sorting to a method which does not alter the input array and returns you a new (sorted) array:

// Don't forget to include this at the top of the file
using System.Linq;

string[] sorted = Output.OrderBy(x => x).ToArray();

Both of these solutions use LINQ, which makes array (and list) operations much nicer to read (IMHO).

  • Related